gpt4 book ai didi

Python:引发异常类(消息,屏幕,堆栈跟踪)selenium.common.exceptions.UnexpectedAlertPresentException:警报文本:无消息:

转载 作者:行者123 更新时间:2023-12-04 18:44:56 25 4
gpt4 key购买 nike

所以我在我的python项目中一直面临这个问题;每当我的页面上出现 javascript 警报时(在使用 ubuntu linux 的 python CLI 解释器时),所有其他指令语句都会向我抛出此错误

raise exception_class(message, screen, stacktrace) selenium.common.exceptions.UnexpectedAlertPresentException: Alert Text: None Message:



我的意思是简单的指令,例如 browser.current_url 以及复杂的指令。

如果我尝试切换到警报并使用

browser.switch_to.alert.accept()



出现此错误:

raise exception_class(message, screen, stacktrace) selenium.common.exceptions.NoAlertPresentException: Message: No modal dialog is currently open



我通常最终关闭浏览器并重新开始测试。这是有压力的。

我正在使用 ubuntu linux 16.04 LTS + python 2.7 + selenium 版本 ='3.4.3'

最佳答案

怀疑是因为警报没有立即显示,并且切换警报比正在显示的警报完成得更快,您最终会遇到 NoAlertPresentException。

也许你可以试试这个:

from selenium import webdriver
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.common.exceptions import TimeoutException

...
browser.find_element_by_id('show-alert').click()

try:
WebDriverWait(browser, 5).until(EC.alert_is_present(), 'Timed out waiting for alerts to appear')
alert = browser.switch_to.alert
alert.accept()
except TimeoutException:
print ("Timeout and No Alert Appearing")

关于Python:引发异常类(消息,屏幕,堆栈跟踪)selenium.common.exceptions.UnexpectedAlertPresentException:警报文本:无消息:,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51554811/

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