Python Forum
Python / Selenium Turning Off Alert - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: Web Scraping & Web Development (https://python-forum.io/forum-13.html)
+--- Thread: Python / Selenium Turning Off Alert (/thread-20543.html)



Python / Selenium Turning Off Alert - graham23s - Aug-17-2019

Hi Guys,

I'm using Selenium to interact with a few websites, on a few i get this alert:

[Image: TrgBkML.png]

is there a way i can disable all alerts? i have managaed to disable notifications, my code for initializing the driver is below.

# init driver ...
def init_driver(using_linux, proxy):
    options = Options()
    options.headless = False
    options.add_argument('start-maximized')
    options.add_argument('--disable-notifications')
    options.add_argument('--disable-extensions')
    options.add_argument('--log-level=3') 
    options.add_argument('--ignore-certificate-errors')
    options.add_argument('--ignore-ssl-errors')
    options.add_experimental_option("excludeSwitches" , ["enable-automation","load-extension"])
    prefs = {'profile.default_content_setting_values.notifications' : 2}
    options.add_experimental_option('prefs', prefs)
    if proxy == "0.0.0.0:0":
        print("--> PROXY DISABLED ...")
    else:
        print("--> USING PROXY: " + str(proxy) + " ...")
        options.add_argument('--proxy-server=%s' % proxy)
    if using_linux:
        return webdriver.Chrome("/usr/bin/chromedriver", chrome_options=options)
    else:
        return webdriver.Chrome("chromedriver.exe", chrome_options=options)
Any help would be appreciated, the script eventually stalls /breaks because of it.

best regards

Graham


RE: Python / Selenium Turning Off Alert - Larz60+ - Aug-18-2019

This site explains it pretty well: https://www.guru99.com/alert-popup-handling-selenium.html


RE: Python / Selenium Turning Off Alert - graham23s - Aug-18-2019

Thank you, what i did try was:

driver.execute_script("window.onbeforeunload = function() {};")
And this seeems to work well.

regards

Graham


RE: Python / Selenium Turning Off Alert - metulburr - Aug-18-2019

You could also probably do this. Untested though.
fp = webdriver.FirefoxProfile()
fp.set_preference("dom.disable_beforeunload", True)