简体   繁体   English

使用 selenium Python 运行 execute_script 的正确方法是什么?

[英]What is the correct way to run execute_script with selenium Python?

I want to automatically log in to Discord using a JS code that needs to be pasted into the browser console.我想使用需要粘贴到浏览器控制台的JS代码自动登录Discord。 I'm just learning Python and have absolutely no knowledge of JS.我只是在学习 Python 并且对 JS 完全一无所知。 Here is the code to paste into the console:这是粘贴到控制台的代码:

function login(token) {
    setInterval(() => {
      document.body.appendChild(document.createElement `iframe`).contentWindow.localStorage.token = `"${token}"`
    }, 50);
    setTimeout(() => {
      location.reload();
    }, 2500);
  }

login(token);

Here is my failed attempt:这是我失败的尝试:

token = 'token'
driver.execute_script('return function login(token) {setInterval(() => {document.body.appendChild(document.createElement `iframe`).contentWindow.localStorage.token = `"${token}"`}, 50);setTimeout(() => {location.reload();}, 2500);}login('+"'"+f'{token}'+"');")

Received an error:收到错误:

selenium.common.exceptions.JavascriptException: Message: javascript error: Unexpected identifier 'login'
token = 'token'

js = 'function login(token) {setInterval(() => {document.body.appendChild(document.createElement `iframe`).contentWindow.localStorage.token = `"${token}"`}, 50);setTimeout(() => {location.reload();}, 500);}'
time.sleep(3)
driver.execute_script(js + f'login("{token}")')

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Selenium 中的 execute_script() 有什么作用 - What does execute_script() in Selenium does Python和Selenium以“execute_script”来解决“ElementNotVisibleException” - Python and Selenium To “execute_script” to solve “ElementNotVisibleException” Python Selenium如何在带参数的JavaScript元素上执行execute_script - Python selenium how to execute execute_script on a JavaScript element with arguments 带换行符的 selenium execute_script - selenium execute_script with newlines 通过Selenium和Python通过WebDriver实例调用execute_script()方法时,arguments [0]是什么? - What is arguments[0] while invoking execute_script() method through WebDriver instance through Selenium and Python? 更改 src 属性 execute_script selenium python - Change src attribute execute_script selenium python Python Selenium:execute_script 是否可以等待 javascript 内容加载? - Python Selenium : is execute_script able to wait for javascript content to load? 如何在 Selenium Python execute_script 中获取返回元素的值? - How to get value of returned element in Selenium Python execute_script? Python selenium execute_script ajax 请求未返回响应 - Python selenium execute_script ajax request not returning response 通过Selenium在Python中运行带有变量的execute_script - Running execute_script with variable inside in Python via Selenium
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM