问题
This question deals with setting the UnexpectedAlertBehaviour of a Selenium webdriver in Java. How do you do the same thing in Python's ChromeDriver?
I have tried the following;
options = ChromeOptions()
options.headless = True
options.set_capability("UNEXPECTED_ALERT_BEHAVIOUR", "ACCEPT")
options.set_capability("unexpectedAlertBehaviour", "accept")
options.set_capability("CapabilityType.UNEXPECTED_ALERT_BEHAVIOUR", "ACCEPT")
options.set_capability("UnexpectedAlertBehaviour", "ACCEPT")
webdriver.DesiredCapabilities.CHROME["unexpectedAlertBehaviour"] = "accept"
cls.driver = webdriver.Chrome(chrome_options=options)
However, I am still randomly experiencing this unexpectedalertpresent exception:
selenium.common.exceptions.UnexpectedAlertPresentException: Alert Text: Message: unexpected alert open: {Alert text : }
When I run the browser in non headless mode (head mode?) I see no such alerts, but the test still randomly fails with this exception, despite my efforts to set this elusive option.
回答1:
As chromedriver becoming W3C compliant . We need use unhandledPromptBehavior Checked on ChromeDriver 76.0.3809.126 (Runs in W3C standard compliant mode by default)
chrome_options = Options()
chrome_options.set_capability('unhandledPromptBehavior', 'accept')
driver = webdriver.Chrome(options=chrome_options)
driver.get("https://www.google.com")
driver.execute_script('alert(\"HI\");')
time.sleep(10)
print(driver.title)
time.sleep(10)
Reference Chromedriver: Issue 2597: Support new unhandledPromptBehavior modes
来源:https://stackoverflow.com/questions/57700388/how-to-set-unexpectedalertbehaviour-in-selenium-python