Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/283.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python Webdriver错误:";“不存在警报”;在抛出UnexpectedAlertPresentException之后_Python_Selenium_Webdriver_Selenium Webdriver - Fatal编程技术网

Python Webdriver错误:";“不存在警报”;在抛出UnexpectedAlertPresentException之后

Python Webdriver错误:";“不存在警报”;在抛出UnexpectedAlertPresentException之后,python,selenium,webdriver,selenium-webdriver,Python,Selenium,Webdriver,Selenium Webdriver,我正在尝试测试我正在开发的一个网络应用程序。我正在对Firefox22.0使用Firefox驱动程序 有时,可能会弹出一个模式对话框(Javascript提示符()。如果有,我想输入一些文本,然后取消它(单击“确定”) 以下是相关代码: try: if button.text == "Run": button.click() except UnexpectedAlertPresentException: alert = self.driver.switch_to_

我正在尝试测试我正在开发的一个网络应用程序。我正在对Firefox22.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()
正在抛出
意外的AlertPresentException
。但是,只要它尝试执行
print alert.text
,我就会得到:

`NoAlertPresentException: Message: u'No alert is present'`.
如果我删除打印语句,它会在
警报时爆炸。使用以下命令发送\u键

`WebDriverException: Message: u'fxdriver.modals.find_(...) is null'`
我不明白。根据定义,
NoAlertPresentException
是否与抛出并导致首先执行except块的
UnexpectedAlertPresentException
相矛盾

编辑:而且,在我的一生中,我都找不到任何关于
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' 

警戒线上。发送_键('8080')
。我想我不明白为什么
switch\u to\u alert()
在没有警报的情况下不会抛出
NoAlertPresent
,我假设
WebDriverException
表明了这一点。

我认为Selenium关闭了意外警报。显然,您可以更改firefox驱动程序处理意外警报的方式:

作为替代方案,您可以在采取行动之前检查是否存在警报(毕竟,如果您希望处理警报,这并非意外),例如(Java):


我可能不是最好的python程序员,因为我一周前就开始使用它了。 我已经设法创建了一个小功能,可以接受任何警报,也可以执行更多警报

在第2行(从尾部算起的第二行)切换IF,我们还可以处理连续的确认/警报。 如果可以处理confirm()的答复,则使用 在您可以处理所有警报时使用()。 如果警报处于超时状态,则必须在正确的时间尝试“绕过”警报()

我已经设法添加了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>

var c=确认(“你喜欢开玩笑吗?”)
如果(c==true){
警惕(“真实”)
}否则{
警惕(“你是开玩笑的大师,另一个警惕!”)
}
var c=确认(“你喜欢开玩笑吗?”)
如果(c==true){
警惕(“真实”)
}否则{
警惕(“你是开玩笑的大师,另一个警惕!”)
}
log(“运行超时测试”)
setTimeout(函数(){
var c=确认(“你喜欢开玩笑吗?”)
如果(c==true){
警惕(“真实”)
}否则{
警惕(“你是开玩笑的大师,另一个警惕!”)
}
}, 5000)
实际上,WHILE或IF处理整个页面,我猜是因为超时。如果你把一切都放在一起,它会的


我非常确信,这可以通过使用隐式等待和预期条件,用最短的代码来完成。如果您查看警报的来源,您只会使用try:block并返回true/false。

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

我可以告诉你的是,你将得到的不仅仅是一种类型的错误。因此,为了让更多的道路勇士,你必须尝试处理2或3个最常见的问题,如超时、不存在或意外。您必须从selenium.common.exceptions中导入它们,并添加一个Except-NameOfError块,然后对其进行处理。我今天看到,我可以放不止一个,除了,而且还学会了从您的代码示例中传递:)热爱python并每天学习它。您必须用Try block来包围“alert=self.driver.switch_to_alert()”。您正在强制脚本更改为可能不在此处的警报。试试这个:我认为问题在于异常处理程序会从浏览器中删除警报。这有点违反直觉。这可能是根本原因吗?对fxdriver.modals.clearFlag的绕过调用\u。。。查阅
<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>