How to disable a “Reload site? Changes you made may not be saved” popup for (python) selenium tests in chrome?

前端 未结 2 518
再見小時候
再見小時候 2020-11-28 16:16

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

Is it possible to

相关标签:
2条回答
  • 2020-11-28 16:36

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


    onbeforeunload

    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.


    Solution

    There are different strategies available to handle this popup.

    • 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 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)
      
    • 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 based solution:

      $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

    0 讨论(0)
  • 2020-11-28 16:43

    I know this doesn't answer the suppression part of the question, but..

    Try this piece of code for accepting the popup:

    driver.SwitchTo().Alert().Accept();

    0 讨论(0)
提交回复
热议问题