How to disable a "Reload site? Changes you made may not be saved" popup for (python) selenium tests in chrome?
Solution 1
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 asfunction() {};
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
Solution 2
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();
Related videos on Youtube
Alex
Updated on October 18, 2022Comments
-
Alex over 1 year
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 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?
-
Alex about 5 yearsYes I guess I will use this suggestion. But the alert pops up only sometimes.
-
RegCent about 5 yearsYou could try adding "Try Except" block to try to switch to the alert window
-
Alex about 5 yearsI just realized I have this flag
--disable-popup-blocking
already for Chrome. However, it does not seem to work properly. I still get the popup from time to time (repeating the exact same webpage). -
undetected Selenium about 5 yearsDid you tryout the Cross Browser solution? Can you get me a status please?
-
Alex about 5 yearswhen do I have to execute the script? Just one time after I created the driver? Each time for a new page?
-
Alex about 5 yearsPopup still appears sometimes.
-
undetected Selenium about 5 yearsPlease ignore my previous comment. Whereever you are browsing away from a page and this popup appears you need to add this line to get rid of it.