gpt4 book ai didi

python - 如何在 Selenium Python 中设置 UnexpectedAlertBehaviour

转载 作者:行者123 更新时间:2023-12-04 12:42:01 24 4
gpt4 key购买 nike

This question处理在 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)

但是,我仍然随机遇到这个unexpectedalertpresent异常:

selenium.common.exceptions.UnexpectedAlertPresentException: Alert Text: Message: unexpected alert open: {Alert text : }



当我以非 headless 模式(头模式?)运行浏览器时,我看不到这样的警报,但尽管我努力设置这个难以捉摸的选项,但测试仍然随机失败并出现此异常。

最佳答案

随着 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: Issue 2597: Support new unhandledPromptBehavior modes

关于python - 如何在 Selenium Python 中设置 UnexpectedAlertBehaviour,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57700388/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com