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

时间:2013-07-10 16:35:06

标签: 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()

UnexpectedAlertPresentException 被抛出。但是,只要它尝试执行print alert.text,我就会得到:

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

如果我删除了print语句,它会在alert.send_keys处以:

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

我不明白。根据定义,NoAlertPresentException是否与抛出的UnexpectedAlertPresentException相矛盾并导致在第一个位置执行except块?

编辑:另外,我不能在我的生活中找到关于http://selenium.googlecode.com/svn/trunk/docs/api/py/index.html#documentation

UnexpectedAlertPresentException的任何文档

编辑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指示的内容。

3 个答案:

答案 0 :(得分: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
}

答案 1 :(得分:2)

我可能不是最好的python程序员,因为我在1周前开始使用它。 我已经设法创建一个可以接受任何警报的小功能,并且还会发出更多警报。

在第-2行(从尾部开始)将IF转换为WHILE,我们也可以处理连续的确认/警报。 使用IF你可以处理confirm()的回复 使用WHILE可以处理所有警报()。 如果警报超时,则必须在正确的时刻尝试“绕过”警报()。

我已设法添加2个异常处理程序,以绕过未知警报(),并在没有alert()时停止。

import selenium
from selenium import webdriver
import os
import time
from selenium.common.exceptions import UnexpectedAlertPresentException
from selenium.common.exceptions import NoAlertPresentException

os.system("pkill php")
os.system("php -S localhost:2222 alert.html &")

fire = webdriver.Firefox()
fire.get("http://localhost:2222")
global alert

def alert_accept():
  try:
    alert = fire.switch_to_alert()
    print "Aler text:" + alert.text
    alert.accept()
    print "Alert detected, accept it"
    return True
  except UnexpectedAlertPresentException:
    print "Hum..., continue?"
    return False
  except NoAlertPresentException:
    print "No alert here"
    return False

while alert_accept() == True:
  alert_accept()

您无法使用任何网站对其进行测试。我已经制作了一个带有一些不同警报的本地HTML,以便对此进行一些挖掘。

HTML code:

<script type="text/javascript">
var c = confirm("Do you like kidding yourself?")
if (c == true) {
  alert("true")
} else {
  alert("You're the kidding master, another alert!")
}
</script>

<script type="text/javascript">
var c = confirm("Do you like kidding yourself?")
if (c == true) {
  alert("true")
} else {
  alert("You're the kidding master, another alert!")
}
</script>

<script type="text/javascript">
console.log("Running test with timeout")
</script>

<script type="text/javascript">
setTimeout(function(){ 
  var c = confirm("Do you like kidding yourself?")
if (c == true) {
  alert("true")
} else {
  alert("You're the kidding master, another alert!")
}
 }, 5000)
</script>

实际上,WHILE和IF处理整页,我猜是因为超时。如果你全力以赴的话。

我很确定这可以使用隐式等待和具有最短代码的预期条件来完成。如果您看一下alert_is_present的来源,那么除了try:block之外,你将只返回true / false。

答案 2 :(得分:1)

对于我的情况,我需要点击“上传”按钮而不选择“文件”并检查是否有警报消息。 但在我的情况下,点击上传按钮时会出现以下异常     selenium.common.exceptions.UnexpectedAlertPresentException:警告文本:     消息:存在模态对话框 在我的情况下,alert.accept或alert.dismiss不起作用 所以,我按Tab键并选择Upload按钮并按下Enter Code键通过Python Code。它的工作完美。