该问题涉及在Java中设置Selenium Webdriver的UnexpectedAlertBehaviour。您如何在Python的ChromeDriver中做同样的事情?
我尝试了以下方法;
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)
但是,我仍然随机遇到此意外的异常提示异常:
selenium.common.exceptions.UnexpectedAlertPresentException:警报文本:消息:意外警报打开:{Alert text:}
当我在非无头模式(头模式?)下运行浏览器时,没有看到这样的警报,但是尽管我努力设置了这个难以捉摸的选项,但测试仍然随机失败,并带有此异常。
随着chromedriver成为W3C兼容。我们需要用unhandledPromptBehavior经过上ChromeDriver 76.0.3809.126(默认情况下W3C标准兼容模式下运行)
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)
参考Chromedriver:问题2597:支持新的unhandledPromptBehavior模式
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句