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:

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?

undetected Selenium
  • 183,867
  • 41
  • 278
  • 352
Alex
  • 41,580
  • 88
  • 260
  • 469

3 Answers3

8

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

undetected Selenium
  • 183,867
  • 41
  • 278
  • 352
  • 6
    I 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). – Alex Apr 09 '19 at 10:54
  • 1
    when do I have to execute the script? Just one time after I created the driver? Each time for a new page? – Alex Apr 09 '19 at 11:04
  • Popup still appears sometimes. – Alex Apr 09 '19 at 11:09
  • Please expand this to include where to find ChromeOptions(); or where / how to implement the jQuery solution? Where would I put the jQuery soluion? $I is not defined. – MaxRocket Nov 21 '22 at 16:52
  • As of today, the Chrome's solution does not seem to have any influence in avoiding the appearance of alert popups. – Eduardo Matsuoka May 18 '23 at 15:02
3

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

RegCent
  • 98
  • 11
0

For java, you can usee the following code,

// Set the path to the ChromeDriver executable
System.setProperty("webdriver.chrome.driver", "path_to_chromedriver");

// Create ChromeOptions instance
ChromeOptions options = new ChromeOptions();

// Disable pop-ups
options.addArguments("--disable-popup-blocking");

// Create WebDriver instance
WebDriver driver = new ChromeDriver(options);

// Rest of your code...

Hope this helps. Thanks.

Manuli
  • 1,203
  • 4
  • 20
  • 43