13

我正在尝试测试我正在开发的 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并导致异常块首先被执行?

编辑:另外,我无法UnexpectedAlertPresentExceptionhttp://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指示。

4

3 回答 3

3

我认为 Selenium 会关闭意外警报。显然,您可以更改 Firefox 驱动程序处理意外警报的方式: 如何在 Selenium 中处理具有“UnexpectedAlertBehaviour”功能的警报?

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

try {
  Alert alert = _driver.switchTo().alert();
  //do stuff with alert
} catch (final NoAlertPresentException e) {
  //do non-alert stuff
}
于 2013-07-10T17:45:06.313 回答
2

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

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

我设法添加了 2 个异常处理程序,以绕过 uknown alerts() 并在不存在 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 代码:

<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 的来源,您只会看到一个返回 true/false 的 try:block。

于 2015-06-09T07:46:38.897 回答
1

对于我的情况,我需要在不选择文件的情况下单击上传按钮,并检查是否有警报消息出现。但在我的情况下,当点击上传按钮时,下面的异常即将到来 selenium.common.exceptions.UnexpectedAlertPresentException:警报文本:消息:模态对话框在我的情况下,alert.accept 或 alert.dismiss 不起作用所以,我确实按了 Tab并选择上传按钮并通过 Python 代码按 Enter 键。它的工作完美。

于 2017-06-16T07:53:20.800 回答