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

时间:2018-07-27 09:23:10

标签: python selenium ubuntu

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

  

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

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

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

接受它
  

browser.switch_to.alert.accept()

出现此错误:

  

引发exception_class(消息,屏幕,堆栈跟踪)   selenium.common.exceptions.NoAlertPresentException:消息:当前未打开任何模式对话框

我通常最终关闭浏览器,然后重新开始测试。这很压力。

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

1 个答案:

答案 0 :(得分:0)

可能是因为没有立即显示警报,并且切换警报比显示的警报要快,所以您最终将出现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")