我正在尝试测试我正在开发的 webapp。我正在使用针对 Firefox 22.0 的 Firefox 驱动程序。
在某一时刻,可能会弹出一个模式对话框(Javascript prompt())。如果是这样,我想输入一些文本,然后将其关闭(单击确定)。
以下是相关代码:
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
,我就会得到:
`NoAlertPresentException: Message: u'No alert is present'`.
如果我删除打印语句,它会爆炸alert.send_keys
:
`WebDriverException: Message: u'fxdriver.modals.find_(...) is null'`
我不明白。定义是否与抛出NoAlertPresentException
的内容相矛盾UnexpectedAlertPresentException
并导致异常块首先被执行?
编辑:另外,我无法UnexpectedAlertPresentException
在http://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
但是,我仍然看到这个:
WebDriverException: Message: u'fxdriver.modals.find_(...) is null'
就行了alert.send_keys('8080')
。我想我不明白如果没有警报为什么switch_to_alert()
不抛出NoAlertPresent
......这就是我假设的WebDriverException
指示。