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
}
相关文章