【问题标题】:Python: raise exception_class(message, screen, stacktrace) selenium.common.exceptions.UnexpectedAlertPresentException: Alert Text: None Message:Python:引发异常类(消息,屏幕,堆栈跟踪)selenium.common.exceptions.UnexpectedAlertPresentException:警报文本:无消息:
【发布时间】:2019-09-23 13:51:01
【问题描述】:

所以我在我的 python 项目中一直面临这个问题;每当我的页面上出现 javascript 警报时(在使用 ubuntu linux 的 python CLI 解释器时),所有其他指令语句都会向我抛出此错误

引发异常类(消息、屏幕、堆栈跟踪) selenium.common.exceptions.UnexpectedAlertPresentException:警报文本:无 消息:

我指的是简单的指令,例如 browser.current_url 以及复杂的指令。

如果我尝试切换到警报并使用

接受它

browser.switch_to.alert.accept()

出现此错误:

引发异常类(消息、屏幕、堆栈跟踪) selenium.common.exceptions.NoAlertPresentException:消息:当前没有打开模式对话框

我通常最终关闭浏览器并重新开始测试。这压力很大。

我正在使用 ubuntu linux 16.04 LTS + python 2.7 + selenium 版本 = '3.4.3'

【问题讨论】:

  • 你从哪里下载了这个selenium version = '3.4.3'
  • 你好,我使用 pip 命令安装 selenium。谢谢
  • 3.13.0 是我们拥有的最新版本。
  • 我刚刚更新了我的,还是一样的问题
  • 谁能帮忙?

标签: python selenium ubuntu


【解决方案1】:

怀疑是因为警报没有立即显示,并且切换警报的完成速度比显示的警报快,您最终会遇到 NoAlertPresentException。

也许你可以试试这个:

from selenium import webdriver
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.common.exceptions import TimeoutException

...

browser.find_element_by_id('show-alert').click()

try:
    WebDriverWait(browser, 5).until(EC.alert_is_present(), 'Timed out waiting for alerts to appear')
    alert = browser.switch_to.alert
    alert.accept()
except TimeoutException:
    print ("Timeout and No Alert Appearing")

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-06-02
    • 2022-10-13
    • 1970-01-01
    • 2013-04-08
    • 2014-01-12
    • 1970-01-01
    相关资源
    最近更新 更多