如何禁用“重新加载网站"?您所做的更改可能不会保存" Chrome中的(python)硒测试的弹出窗口? [英] How to disable a "Reload site? Changes you made may not be saved" popup for (python) selenium tests in chrome?

查看:1619
本文介绍了如何禁用“重新加载网站"?您所做的更改可能不会保存" Chrome中的(python)硒测试的弹出窗口?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在chrome浏览器上运行自动化(python)硒测试,有时重新加载页面时,屏幕上会出现一个弹出窗口:

I am running automated (python) selenium tests on a chrome browser, and sometimes when a page is reloaded a popup appears on the screeen:

是否可以通过这种方式配置Chrome硒浏览器,使该弹出窗口不出现?如果是这样,该怎么做?还是有其他方法可以抑制此弹出窗口?还是接受它?

Is it possible to configure the chrome selenium browser in such a way, that this popup does not appear? If so, how to do that? Or are there other ways to supress this popup? Or to accept it?

推荐答案

此弹出窗口,其文本为重新加载网站?您所做的更改可能不会保存,这是

This popup with text as Reload site? Changes you made may not be saved is the implementation of onbeforeunload property of WindowEventHandlers

onbeforeunload 属性href ="https://developer.mozilla.org/zh-CN/docs/Web/API/WindowEventHandlers" rel ="nofollow noreferrer"> WindowEventHandlers mixin是卸载其资源时,会触发这些事件.此时,文档仍然可见,事件仍可取消.

The onbeforeunload property of the WindowEventHandlers mixin is the EventHandler for processing beforeunload events. These events fire when a window is about to unload its resources. At this point, the document is still visible and the event is still cancelable.

有多种策略可用于处理此弹出窗口.

There are different strategies available to handle this popup.

  • Chrome 解决方案:通过 ChromeOptions()使用--disable-popup-blocking:

  • Chrome solution: Using --disable-popup-blocking through ChromeOptions():

from selenium import webdriver

options.add_argument("--disable-popup-blocking")
driver=webdriver.Chrome(chrome_options=options, executable_path=/path/to/chromedriver')

  • Firefox 解决方案:通过 FirefoxProfile()使用dom.disable_beforeunload:

  • Firefox solution: Using dom.disable_beforeunload through FirefoxProfile():

    from selenium import webdriver
    profile = webdriver.FirefoxProfile()
    profile.set_preference("dom.disable_beforeunload", True)
    driver = webdriver.Firefox(firefox_profile = profile)
    

  • 跨浏览器解决方案:作为跨浏览器解决方案,您可以禁用此对话框,调用executeScript()设置 window.onbeforeunload 作为function() {};,您可以使用以下解决方案:

  • Cross Browser solution: As a Cross Browser solution, you can disable this dialog invoking the executeScript() to set window.onbeforeunload as function() {}; and you can use the following solution:

    driver.execute_script("window.onbeforeunload = function() {};")
    

  • 基于jQuery 的解决方案:

  • JQuery based solution:

    $I->executeJS( "window.onbeforeunload = null" );
    

  • 您可以在

    这篇关于如何禁用“重新加载网站"?您所做的更改可能不会保存" Chrome中的(python)硒测试的弹出窗口?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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