繁体   English   中英

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

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

我想使用需要粘贴到浏览器控制台的JS代码自动登录Discord。 我只是在学习 Python 并且对 JS 完全一无所知。 这是粘贴到控制台的代码:

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

login(token);

这是我失败的尝试:

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}'+"');")

收到错误:

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM