【发布时间】:2019-04-17 19:26:35
【问题描述】:
使用 Python 3 和 Selenium > Chromedriver。
我正在运行一个 python 脚本来从不同的网站上抓取内容。
我需要打开和关闭很多不同的网站。
问题:
我的程序在发出警报时崩溃。
selenium.common.exceptions.UnexpectedAlertPresentException:警报文本:无 消息:意外警报打开
我尝试做什么来解决这个问题?
当我执行 driver.get() 时,我立即运行:
driver.execute_script("window.alert = null;")
driver.execute_script("Window.prototype.alert = null;")
在执行 driver.close() 之前,我运行:
driver.execute_script("window.onbeforeunload = null;")
在 driver.close() 之后,我就这样做了:
from selenium.common.exceptions import NoAlertPresentException
...
driver.close()
# any alert asking me if I really want to close?
try:
driver.switch_to.alert.accept()
except NoAlertPresentException:
pass
我的目标是什么?
没什么,程序老是崩溃:
问题:
我该如何设置整个事情,以禁用每个警报。
如果不可能,我怎样才能全部接受它们或至少接受它们以使程序继续运行?
【问题讨论】:
-
我在访问该站点时没有看到弹出窗口。你能翻译一下弹出窗口的全部内容吗?谢谢
-
@DebanjanB 这是典型的“你确定要离开这个页面吗?”弹出。而且它只是偶尔出现。
-
您是否使用相同的驱动程序实例浏览到另一个 url?我可以看到你使用
driver.close()为什么不用driver.quit()?谢谢 -
@DebanjanB 我使用退出的驱动程序关闭插入,以便我可以继续使用相同的驱动程序;)