2one2
2one2

Reputation: 401

how can i remove notifications and alerts from browser? selenium python 2.7.7

I am trying to submit information in a webpage, but selenium throws this error:

UnexpectedAlertPresentException: Alert Text: This page is asking you to confirm that you want to leave - data you have entered may not be saved. , >

It's not a leave notification; here is a pic of the notification -

enter image description here.

If I click in never show this notification again, my action doesn't get saved; is there a way to save it or disable all notifications?

edit: I'm using firefox.

Upvotes: 3

Views: 20069

Answers (6)

krchun
krchun

Reputation: 1044

For Google Chrome and v3 of Selenium you may receive "DeprecationWarning: use options instead of chrome_options", so you will want to do the following:

from selenium import webdriver
from webdriver_manager.chrome import ChromeDriverManager

options = webdriver.ChromeOptions()
options.add_argument('--disable-notifications')
driver = webdriver.Chrome(ChromeDriverManager().install(), options=options)

Note: I am using webdriver-manager, but this also works with specifying the executable_path.

Upvotes: 1

Khushhal
Khushhal

Reputation: 653

This answer is an improvement on TH Todorov code snippet, based on what is working as of Chrome (Version 80.0.3987.163).

lk = os.path.join(os.getcwd(), "chromedriver",) --> in this line you provide the link to the chromedriver, which you can download from chromedrive link

import os

from selenium import webdriver

lk = os.path.join(os.getcwd(), "chromedriver",)
chrome_options = webdriver.ChromeOptions()
prefs = {"profile.default_content_setting_values.notifications" : 2}
chrome_options.add_experimental_option("prefs",prefs)
driver = webdriver.Chrome(lk, options=chrome_options)

Upvotes: 0

Obie
Obie

Reputation: 111

This will do it:

from selenium.webdriver.firefox.options import Options
options = Options()
options.set_preference("dom.webnotifications.enabled", False)
browser = webdriver.Firefox(firefox_options=options)

Upvotes: 1

Anvesh
Anvesh

Reputation: 149

With the latest version of Firefox the above preferences didn't work.

Below is the solution which disable notifications using Firefox object

_browser_profile = webdriver.FirefoxProfile()
_browser_profile.set_preference("dom.webnotifications.enabled", False)
webdriver.Firefox(firefox_profile=_browser_profile)

Disable notifications when using Remote Object: webdriver.Remote(desired_capabilities=_desired_caps, command_executor=_url, options=_custom_options, browser_profile=_browser_profile)

selenium==3.11.0

Upvotes: 11

TH Todorov
TH Todorov

Reputation: 1139

You can disable the browser notifications, using chrome options. Sample code below:

chrome_options = webdriver.ChromeOptions()
prefs = {"profile.default_content_setting_values.notifications" : 2}
chrome_options.add_experimental_option("prefs",prefs)
driver = webdriver.Chrome(chrome_options=chrome_options)

Upvotes: 24

Jashan
Jashan

Reputation: 51

Usually with browser settings like this, any changes you make are going to get throws away the next time Selenium starts up a new browser instance.

Are you using a dedicated Firefox profile to run your selenium tests? If so, in that Firefox profile, set this setting to what you want and then close the browser. That should properly save it for its next use. You will need to tell Selenium to use this profile though, thats done by SetCapabilities when you start the driver session.

Upvotes: 2

Related Questions