如何在Selenium Python中设置UnexpectedAlertBehaviour [英] How to set UnexpectedAlertBehaviour in Selenium Python

查看:245
本文介绍了如何在Selenium Python中设置UnexpectedAlertBehaviour的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

此问题涉及设置Java中的Selenium Webdriver的UnexpectedAlertBehaviour.您如何在Python的ChromeDriver中做同样的事情?

This question deals with setting the UnexpectedAlertBehaviour of a Selenium webdriver in Java. How do you do the same thing in Python's 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)

但是,我仍然随机遇到此意外的存在的异常:

However, I am still randomly experiencing this unexpectedalertpresent exception:

selenium.common.exceptions.UnexpectedAlertPresentException:警报文本: 消息:意外警报打开:{警告文本:}

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.

推荐答案

随着chromedriver兼容W3C.我们需要使用 unhandledPromptBehavior 在ChromeDriver 76.0.3809.126上进行了检查(默认情况下以W3C标准兼容模式运行)

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)

参考Chromedriver:问题2597 :支持新的unhandledPromptBehavior模式

Reference Chromedriver: Issue 2597: Support new unhandledPromptBehavior modes

这篇关于如何在Selenium Python中设置UnexpectedAlertBehaviour的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆