Python 和 Selenium - 离开页面时禁用警报

标签 python selenium automation

使用 Python 3Chromedriver

假设一个自动化的 Python 程序正在网上冲浪,从不同的来源获取内容。

假设其中任何一个网站触发“您确定要离开此页面吗?”警报。

关键字:这些网站的任何(以随机方式)。

问题:

我如何设置程序来处理这些警报,总是说:“是的,我想离开这个页面”。?

---更新---

可能的方法:

根据下面的评论我现在正在做:

def super_get(url):
    driver.get(url)
    driver.execute_script("window.onbeforeunload = function() {};")

现在使用 super_get() 插入标准 driver.get()

您能想出更有效或更清洁的方法吗?

最佳答案

卸载前禁用警报:

def super_get(url):
    driver.get(url)
    driver.execute_script("window.onbeforeunload = function() {};")

现在使用 super_get(whatever_url) insetad 标准 driver.get(whatever_url)

禁用页面中的所有警报:

def super_get(url):
    driver.get(url)
    driver.execute_script("window.alert = function() {};")

希望对大家有所帮助。干杯。

关于Python 和 Selenium - 离开页面时禁用警报,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45288215/

相关文章:

automation - 用代码创建一个 jmx 文件

python - 如何在列表上使用 reduce() 并获取字典?

python - 如何以及在何处通过基于 macOS Python 的应用程序上的 native GUI 最好地检索 sudo 密码 - (同时维护交互式输出流(stdout))

python - 挑选数据库后进行身份验证

javascript - Dropzone.js : Upload file without Browse dialog in php-webdriver integration tests

java - 无法运行testng程序

python - 我应该使用哪种方法来访问文件,为什么?

python - 使用 Selenium Webdriver 下载时命名文件

angularjs - 在 Protractor 测试中从 localStorage 中删除项目

ios - 是否可以在 iOS 中同时自动化两个应用程序?