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))
Try changing
to