Handle notifications in Python + Selenium Chrome WebDriver
Asked Answered
E

6

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

Ectoenzyme answered 30/12, 2016 at 18:28 Comment(0)
S
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)
Solberg answered 30/12, 2016 at 18:36 Comment(2)
but I just want to disable xdg-open,NOT all the notifications.How could I solve it?Lode
in 2023 still working :DIndus
P
7

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()
Pruter answered 9/12, 2021 at 10:30 Comment(0)
S
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

Synaeresis answered 22/11, 2020 at 6:27 Comment(0)
G
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.

Graiae answered 7/8, 2020 at 8:53 Comment(0)
B
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

Bollard answered 27/8, 2020 at 14:33 Comment(0)
B
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.

Barbusse answered 14/12, 2023 at 20:4 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.