10

How can Selenium Chrome WebDriver notifications be handled in Python?

Have tried to dismiss/accept alert and active element but seems notifications have to be treated other way. Also, all the Google search results are driving me to Java solution which I do not really need. I'm a newbie in Python.

Thanks in advance.

enter image description here

6 Answers 6

37

You can disable the browser notifications, using chrome options.

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)
Sign up to request clarification or add additional context in comments.

2 Comments

but I just want to disable xdg-open,NOT all the notifications.How could I solve it?
in 2023 still working :D
8

In December, 2021, using ChromeDriver Version: 96, the python code structure would look like below to handle the ChromeDriver/ Browser notification:

from selenium import webdriver    
from selenium.webdriver.chrome.options import Options

# Creating Instance
option = Options()

# Working with the 'add_argument' Method to modify Driver Default Notification
option.add_argument('--disable-notifications')

# Passing Driver path alongside with Driver modified Options
browser = webdriver.Chrome(executable_path= your_chrome_driver_path, chrome_options= option)

# Do your stuff and quit your driver/ browser
browser.get('https://www.google.com')
browser.quit()

Comments

2

Follow the below code for handling the notification settings with Python selenium

from selenium import webdriver


from selenium.webdriver.chrome.options import Options

option = Options()

option.add_argument("--disable-infobars")

option.add_argument("start-maximized")

option.add_argument("--disable-extensions")

option.add_experimental_option("prefs", 
{"profile.default_content_setting_values.notifications": 2 
 }) 

Note 1-to allow notification, 2- to block notification

Comments

1

The answer above from Dec '16 didn't work for me in August 2020. Both Selenium and Chrome have changed, I believe.

I'm using the binary for Chrome 84, which is listed as the current version, even though there's a binary for Chrome 85 available from selenium.dev. I have to presume that's a beta since I have no other info on that.

I only have a partial answer so far, but I believe the following two lines are in the right direction. They're as far as I've gotten tonight. I have a test window that I've opened to Facebook and have the exact window shown in the Question. The code given in pythonjar's answer from Dec 30 '16 above produced error messages for me. These code lines worked without error, so I believe this is the right track.

>>> from selenium.webdriver import ChromeOptions
>>> chrome_options = ChromeOptions()

I'll update this tomorrow, if I get time to work on it.

Sorry for the partial answer. I hope to complete it soon. I hope this helps. If you get there first, please let me know.

Comments

1

Using Chrome Versão 85.0.4183.83 (Versão oficial) 64 bits and ChromeDriver 85.0.4183.83

Here is the code and it is working:

from selenium import webdriver

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)
driver.get("https://www.google.com/")

2020/08/27

Comments

0

if you are using selenium 4.x , these are the modifications you would like to consider:

options

from selenium import webdriver

options = webdriver.ChromeOptions()

# Working with the 'add_argument' Method to modify Driver Default Notification
options.add_argument('--disable-notifications')

# Passing Driver path alongside with Driver modified Options
driver = webdriver.Chrome(options=options)

more examples of options


Path

For the path executable_path you can leave it empty, and it will download it automatically in the ~/.cache/selenium

Or use this modification:

from selenium import webdriver
from selenium.webdriver.chrome.service import Service

options = webdriver.ChromeOptions()
service = Service(executable_path=CHROMEDRIVER_PATH)
driver = webdriver.Chrome(service=service, options=options)

This I found it here.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.