简体   繁体   English

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

[英]Python and Selenium - Disable alert when leaving page

Using Python 3 and Chromedriver . 使用Python 3Chromedriver

Suppose an automated python program is surfing the web, fetching stuff from different sources. 假设一个自动化的python程序在网上冲浪,从不同的来源获取东西。

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. 关键词: 任何 (以随机方式)这些网站。

Question : 问题

How may I set up the program to handle those alerts, by always saying: "yes, i want to leave this page".? 我如何设置程序来处理这些警报,总是说:“是的,我想离开这个页面”。

--- UPDATE --- ---更新---

Possible approach : 可能的方法

Based on the comment below I am now doing: 基于以下评论,我现在正在做:

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

And now use super_get() insetad of the standard driver.get() 现在使用标准driver.get() super_get() insetad

Can you think of any more efficient or cleaner way of doing it? 你能想到更有效或更清洁的方式吗?

Disable the alert on before unload : 在卸载之前禁用警报

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

And now use super_get(whatever_url) insetad of the standard driver.get(whatever_url) 而现在使用super_get(whatever_url)标准的insetad driver.get(whatever_url)

Disable all alerts in page : 禁用页面中的所有警报

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

Hope it helps somebody. 希望它对某人有帮助。 Cheers. 干杯。

Goal Get all listeners by getEventListeners(window)['beforeunload'] and remove them all. 目标通过getEventListeners(window)['beforeunload']获取所有侦听器并将其全部删除。

Step1. 第1步。 Put DOMDebugger.getEventListeners to window['getEventListeners'] 把DOMDebugger.getEventListeners放到window ['getEventListeners']

VB.NET VB.NET

Dim myChromeDriver as ChromeDriver = getMyDriver()
myChromeDriver.ExecuteChromeCommand("Runtime.evaluate", New Dictionary(Of String, Object)() From {
{"expression", "window['getEventListeners'] = getEventListeners;"},
{"includeCommandLineAPI", True}})

We don't need the result value of this Command. 我们不需要此Command的结果值。

includeCommandLineAPI=True is required or it will throw exception because getEventListeners is an CommandLineAPI that is undefined in script execution context includeCommandLineAPI = True是必需的,否则它将抛出异常,因为getEventListeners是一个在脚本执行上下文中未定义的CommandLineAPI

Step2. 第2步。 Remove all listeners 删除所有侦听器

Dim jsCmd As String = "var eventlistener = window['getEventListeners'](window)['beforeunload'][0];" &
"    window.removeEventListener('beforeunload', " &
"    eventlistener.listener, " &
"    eventlistener.useCapture); "
myChromeDriver.ExecuteScript(jsCmd)

Run this script until getEventListeners(window)['beforeunload'] is empty. 运行此脚本,直到getEventListeners(window)['beforeunload']为空。

This script will throw exception when all listeners removed, you can fix it. 删除所有侦听器后,此脚本将抛出异常,您可以修复它。

Finally 最后

oDriver.Navigate.GoToUrl("https://www.google.com")

The "Are you sure.." alert should disappear. “你确定......”警报应该消失。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM