Webdriver错误:抛出UnexpectedAlertPresentException后出现"没有警报"

Isa*_*ell 12 python selenium webdriver selenium-webdriver

我正在尝试测试我正在开发的webapp.我正在使用针对Firefox 22.0的Firefox驱动程序.

有一次,可能会弹出一个模态对话框(一个Javascript提示符()).如果是,我想输入一些文本,然后将其关闭(单击确定).

这是相关的代码:

try:
    if button.text == "Run":
        button.click()
except UnexpectedAlertPresentException:
    alert = self.driver.switch_to_alert()
    print alert.text
    alert.send_keys('8080')
    alert.dismiss()
Run Code Online (Sandbox Code Playgroud)

UnexpectedAlertPresentException 抛出.但是,一旦它尝试执行print alert.text,我得到:

`NoAlertPresentException: Message: u'No alert is present'`.
Run Code Online (Sandbox Code Playgroud)

如果我删除了print语句,它就会爆炸alert.send_keys:

`WebDriverException: Message: u'fxdriver.modals.find_(...) is null'`
Run Code Online (Sandbox Code Playgroud)

我不明白.不是NoAlertPresentException通过定义矛盾于UnexpectedAlertPresentException这被抛出,并引起了除块中首先执行?

编辑:另外,我不能为我的生活找到的任何文件UnexpectedAlertPresentExceptionhttp://selenium.googlecode.com/svn/trunk/docs/api/py/index.html#documentation

编辑2:这就是我现在拥有的:

try:
    if button.text == "Run":
        button.click()

        alert = self.driver.switch_to_alert()

        alert.send_keys('1111')
        alert.dismiss()

 except NoAlertPresentException:
     pass
Run Code Online (Sandbox Code Playgroud)

但是,我仍然看到这个:

WebDriverException: Message: u'fxdriver.modals.find_(...) is null' 
Run Code Online (Sandbox Code Playgroud)

就行了alert.send_keys('8080').我想我不明白为什么switch_to_alert()不扔,NoAlertPresent如果没有警报......这是我假设的WebDriverException指示.

小智 3

我认为 Selenium 会关闭意外警报。显然,您可以更改 Firefox 驱动程序处理意外警报的方式: How to handle an Alert with "UnexpectedAlertBehaviour"capability in Selenium?

作为替代方案,您可以在执行操作之前检查是否有警报(毕竟,如果您想处理警报,这并不意外),如下所示(Java):

try {
  Alert alert = _driver.switchTo().alert();
  //do stuff with alert
} catch (final NoAlertPresentException e) {
  //do non-alert stuff
}
Run Code Online (Sandbox Code Playgroud)