Webdriver错误:“没有警报"引发UnexpectedAlertPresentException之后 [英] Webdriver error: "No alert is present" after UnexpectedAlertPresentException is thrown

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

问题描述

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

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

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

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是否不与抛出并导致except块首先执行的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?

另外,我一生无法在这就是我现在拥有的:

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' 

行上的

.我想我不明白为什么如果没有警报,为什么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"处理警报硒的功能?

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之后的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆