Python和Selenium-离开页面时禁用警报 [英] Python and Selenium - Disable alert when leaving page

查看:275
本文介绍了Python和Selenium-离开页面时禁用警报的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用 Python 3 Chromedriver .

假设有一个自动化的python程序正在网上冲浪,并从不同的来源获取东西.

Suppose an automated python program is surfing the web, fetching stuff from different sources.

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

Suppose any of those websites fires an "Are you sure you wanna leave this page?" alert.

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

Key word: any (in a random way) of those websites.

问题:

我如何通过总是说:是的,我要离开此页面"来设置程序来处理那些警报??

How may I set up the program to handle those alerts, by always saying: "yes, i want to leave this page".?

---更新---

可能的方法:

基于下面的评论,我现在正在做

Based on the comment below I am now doing:

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

现在使用标准driver.get()

您能想到任何更有效或更清洁的方法吗?

Can you think of any more efficient or cleaner way of doing it?

推荐答案

在卸载前禁用警报:

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

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

禁用页面中的所有警报:

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

希望它对某人有帮助.干杯.

Hope it helps somebody. Cheers.

这篇关于Python和Selenium-离开页面时禁用警报的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆