使用 Python 3 和 Chromedriver。
假设一个自动化的 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/