Selenium Web驱动程序:意外的AlertpresentException(新问题)



关于同一主题的另一个问题!!

上一个关于关闭警报的问题:Selenium Webdriver:UnexpectedAlertPresentException

我试着关闭警报,但现在它给出了一个错误,说没有这样的警报。

错误:

Traceback (most recent call last):
File "main.py", line 19, in <module>
alert = driver.switch_to.alert.dismiss() 
File "/opt/virtualenvs/python3/lib/python3.8/site-packages/selenium/webdriver/remote/switch_to.py", line 56, in alert
alert.text
File "/opt/virtualenvs/python3/lib/python3.8/site-packages/selenium/webdriver/common/alert.py", line 66, in text
return self.driver.execute(Command.W3C_GET_ALERT_TEXT)["value"]
File "/opt/virtualenvs/python3/lib/python3.8/site-packages/selenium/webdriver/remote/webdriver.py", line 424, in execute
self.error_handler.check_response(response)
File "/opt/virtualenvs/python3/lib/python3.8/site-packages/selenium/webdriver/remote/errorhandler.py", line 247, in check_response
raise exception_class(message, screen, stacktrace)
selenium.common.exceptions.NoAlertPresentException: Message: no such alert
(Session info: chrome=91.0.4472.101)
Stacktrace:
#0 0x55ec4c205919 <unknown>

脚本:

driver.find_element(By.NAME,"nickname").send_keys(username+str(x)+Keys.ENTER)
alert = driver.switch_to.alert.dismiss() 
webdriver.switch_to_alert().dismiss() 

NoAlertPresentException异常是在当前未显示警报的情况下尝试关闭警报时抛出的,因此请检查警报是否存在于您试图解除警报的位置。

如果您不确定是否会发生警报,您可以捕获异常并忽略它:

try:
driver.switch_to.alert.dismiss()
except NoAlertPresentException:
print('No alert present')

相关内容

  • 没有找到相关文章

最新更新