Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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

enter image description here

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?

like image 438
Alex Avatar asked Apr 09 '19 07:04

Alex


People also ask

How do I stop pop ups in selenium Python?

Handling Web Dialog Box/Popup Window using Selenium In Selenium, robot class is used to handle the keyboard and mouse functions. It is used to close the pop-up window. You can get the window handle of the pop-up window using the WindowHandle() function.

How do I stop a website from reloading?

Type chrome://extensions in the URL and Navigate to Options. Go to Details and click on Extension options. Tick the option with Disable Meta Refresh elements in pages and click on Close. Additionally one can disable the automatic tab discarding to stop the tabs from auto-refreshing.


2 Answers

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

like image 103
undetected Selenium Avatar answered Oct 02 '22 14:10

undetected Selenium


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();

like image 25
RegCent Avatar answered Oct 02 '22 13:10

RegCent