gpt4 book ai didi

javascript - 如何为 chrome 中的(python) Selenium 测试禁用 "Reload site? Changes you made may not be saved"弹出窗口?

转载 作者:太空狗 更新时间:2023-10-30 01:10:54 24 4
gpt4 key购买 nike

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

enter image description here

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

最佳答案

此弹出窗口的文本为重新加载站点?您所做的更改可能无法保存WindowEventHandlersonbeforeunload 属性的实现。


onbeforeunload

onbeforeunload WindowEventHandlers 的属性(property)mixin 是 EventHandler加工beforeunload事件。这些事件在窗口即将变为 unload 时触发。它的资源。此时,文档仍然可见,事件仍然可以取消。


解决方案

有不同的策略可用于处理此弹出

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

    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:

    from selenium import webdriver
    profile = webdriver.FirefoxProfile()
    profile.set_preference("dom.disable_beforeunload", True)
    driver = webdriver.Firefox(firefox_profile = profile)
  • 跨浏览器解决方案:作为跨浏览器解决方案,您可以禁用此对话框调用executeScript() 来设置< em>window.onbeforeunload as function() {}; 并且您可以使用以下解决方案:

    driver.execute_script("window.onbeforeunload = function() {};")
  • 基于 JQuery 的解决方案:

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

You can find a relevant discussion in How to handle below Internet Explorer popup “Are you sure you want to leave this page?” through Selenium

关于javascript - 如何为 chrome 中的(python) Selenium 测试禁用 "Reload site? Changes you made may not be saved"弹出窗口?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55587025/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com