Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Handle notifications in Python + Selenium Chrome WebDriver

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

like image 736
Tedo G. Avatar asked Dec 30 '16 18:12

Tedo G.


2 Answers

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)
like image 108
pythonjar Avatar answered Sep 27 '22 22:09

pythonjar


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()
like image 34
Mehedi Hasan Faysal Avatar answered Sep 27 '22 23:09

Mehedi Hasan Faysal