How to disable chrome notifications popup in python and selenium?

Viewed 4658

How to disable chrome notifications popup in python and selenium?

I tried:

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)

But then also it shows notification...

notifications popup image

I tried the same codes answered here but then also I am not able to disable the notifications!

3 Answers

The selenium package has a ChromeOptions class, in which you can add many arguments. One of which is 'disable-notifications'. You can pass that class to the driver class when initializing it.

from selenium import webdriver
chrome_options = webdriver.ChromeOptions()
chrome_options.add_argument('disable-notifications')

driver = webdriver.Chrome('chromedriver.exe', options=chrome_options)

from selenium import webdriver

chrome_options = webdriver.ChromeOptions() chrome_options.add_argument('disable-notifications') driver = webdriver.Chrome(executable_path="C:\Users\PycharmProjects\chromedriver_win32\chromedriver.exe", options=chrome_options) driver.maximize_window()


This works with the right placement of code: Click here for Image-->ChromeOptions Pycharm Code Image

You can pass disable-notifications to your chrome options.

Here's an example I have using Javascript, should work the same with Python.

var o = new chrome.Options();

o.addArguments('user-data-dir=./chromeprofile');
o.addArguments('disable-infobars');
o.addArguments("disable-notifications");
o.setUserPreferences( { credentials_enable_service: false } );
Related