How to set UnexpectedAlertBehaviour in Selenium Python

旧城冷巷雨未停 提交于 2020-07-22 05:17:27

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!