Can't trigger page load event in Selenium

232 Views Asked by At

In the snippet of code below I would expect the program to print True, but instead it prints False. It appears that the event listener isn't being triggered, but I don't understand why.

from selenium import webdriver


javascript_code = f'''
    var x = false;
    window.addEventListener("load", function () {{
        x=true;
    }});

    window.location.href = "https://google.com";
    return x;
'''

driver = webdriver.Chrome()
print(driver.execute_script(javascript_code))
1

There are 1 best solutions below

0
Hrisimir Dakov On

Try changing

javascript_code = f'''

to

javascript_code = '''