Webdriver 错误:“不存在警报"在抛出 UnexpectedAlertPresentException 之后

Webdriver error: quot;No alert is presentquot; after UnexpectedAlertPresentException is thrown(Webdriver 错误:“不存在警报在抛出 UnexpectedAlertPresentException 之后)

本文介绍了Webdriver 错误:“不存在警报"在抛出 UnexpectedAlertPresentException 之后的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I'm trying to test a webapp I'm developing. I'm using the Firefox driver against Firefox 22.0.

在某一时刻,可能会弹出一个模式对话框(Javascript prompt()).如果是这样,我想输入一些文本,然后将其关闭(单击确定).

At one point, a modal dialog may pop up (a Javascript prompt()). If it does, I want to enter some text and then dismiss it (click OK).

以下是相关代码:

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

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

The UnexpectedAlertPresentException is being thrown. However, as soon as it tries to execute print alert.text, I get:

`NoAlertPresentException: Message: u'No alert is present'`.

如果我删除打印语句,它会在 alert.send_keys 处爆炸:

If I remove the print statement, it blows up at alert.send_keys with:

`WebDriverException: Message: u'fxdriver.modals.find_(...) is null'`

我不明白.NoAlertPresentException 根据定义是否与抛出并导致异常块首先执行的 UnexpectedAlertPresentException 相矛盾?

I don't get it. Isn't the NoAlertPresentException by definition contradicting the UnexpectedAlertPresentException that was thrown and caused the except block to be executed in the first place?

另外,我无法在 UnexpectedAlertPresentException 的任何文档/api/py/index.html#documentation">http://selenium.googlecode.com/svn/trunk/docs/api/py/index.html#documentation

Also, I can't for the life of me find any documentation on the UnexpectedAlertPresentException in http://selenium.googlecode.com/svn/trunk/docs/api/py/index.html#documentation

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

Edit 2: This is what I have now:

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

        alert = self.driver.switch_to_alert()

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

 except NoAlertPresentException:
     pass

但是,我仍然看到这个:

However, I'm still seeing this:

WebDriverException: Message: u'fxdriver.modals.find_(...) is null' 

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

on the line alert.send_keys('8080'). I guess I don't understand why switch_to_alert() doesn't throw NoAlertPresent if there isn't an alert...which is what I'm assuming the WebDriverException is indicating.

推荐答案

我认为 Selenium 会关闭意外警报.显然,您可以更改 Firefox 驱动程序处理意外警报的方式:如何使用UnexpectedAlertBehaviour"处理警报Selenium 的能力?

I think Selenium closes unexpected alerts. Apparently you can change how the firefox driver treats unexpected alerts: How to handle an Alert with "UnexpectedAlertBehaviour" capability in Selenium?

作为替代方案,您可以在采取行动之前检查是否有警报(毕竟,如果您想处理警报,这并不意外),就像这样(Java):

As an alternative, you could check if there is an alert before acting (after all, if you want handle the alert it's not unexpected) like so (Java):

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

这篇关于Webdriver 错误:“不存在警报"在抛出 UnexpectedAlertPresentException 之后的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本文标题为:Webdriver 错误:“不存在警报"在抛出 UnexpectedAlertPresentException 之后

基础教程推荐