[英]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.