Python and Selenium - disable alert on page exit

Using Python 3 and Chromedriver .

Suppose an automated python program is running on the internet, pulling stuff from various sources.

Suppose any of these sites display the message "Are you sure you want to leave this page?" warning.

Keywords: any (randomly) of these websites.

Question

How can I configure the program to handle these warnings by always saying "yes, I want to leave this page"?

--- UPDATE ---

Possible approach :

Based on the comment below, I am currently doing:

def super_get(url):
    driver.get(url)
    driver.execute_script("window.onbeforeunload = function() {};")

      

And now use the super_get()

insetad of the standarddriver.get()

Can you think of a better or cleaner way to do this?

+3


source to share


1 answer


Disable warning before unloading :

def super_get(url):
    driver.get(url)
    driver.execute_script("window.onbeforeunload = function() {};")

      

And now use the super_get(whatever_url)

insetad of the standarddriver.get(whatever_url)



Disable all notifications on the page :

def super_get(url):
    driver.get(url)
    driver.execute_script("window.alert = function() {};")

      

Hope this helps someone. Greetings.

+3


source







All Articles