I'm trying to use a javascript code inside the console with selenium python, but I don't know how. This is the function:
function login(token) {
setInterval(() => {
document.body
.appendChild(document.createElement `iframe`)
.contentWindow.localStorage.token = `"${token}"`
}, 50);
setTimeout(() => {
location.reload();
}, 2500);
}
login(token);
I'm truing to use the function inside browser.execute_script() but I don't know how to add it.
You could write a function that creates a script and returns it as a string. You can then call driver.execute_script() to run the script when needed.
This should work:
def generate_login_script(token):
script = """
setInterval(()=>{{document.body.appendChild(document.createElement `iframe`)
.contentWindow.localStorage.token="{token}"}},50);
setTimeout(()=>{{location.reload()}},2500);
""".format(token=token)
return script
driver = webdriver.Chrome("D:\chromedriver\94\chromedriver.exe")
driver.get("https://www.youwebsite.com")
driver.execute_script(generate_login_script("testToken"))
I have tested this on thefamoussearchenginethatstartswithG.com and it works.