9

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

Ratmir Asanov
  • 6,237
  • 5
  • 26
  • 40
Tedo G.
  • 1,556
  • 3
  • 17
  • 29

5 Answers5

36

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)
pythonjar
  • 376
  • 3
  • 3
  • 1
    but I just want to disable xdg-open,NOT all the notifications.How could I solve it? –  Nov 14 '19 at 06:47
6

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

Praveen Kumar C
  • 409
  • 4
  • 6
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.

August
  • 343
  • 3
  • 10
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