所以我在python项目中一直面临这个问题;每当我的页面上出现javascript警报时(在为ubuntu-linux开发python CLI解释器时(,每隔一条指令语句就会向我抛出这个错误
引发exception_class(消息、屏幕、堆栈(selenium.com.mon.exceptions.UnexpectedAlertPresentException:警报文本:无消息:
我的意思是,即使是简单的指令,如browser.current_url,也有复杂的指令。
如果我尝试切换到警报并使用接受它
browser.switch_to.alert.accept((
出现此错误:
引发exception_class(消息、屏幕、堆栈(selenium.com.mon.exceptions.NoAlertPresentException:消息:当前没有打开模式对话框
我通常会关闭浏览器,重新启动测试。这很有压力。
我使用的是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")