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

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

问题描述

使用 Python 3Chromedriver.

假设一个自动化的 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天全站免登陆