Home > Back-end >  Disable Chrome Notifications using Selenium Python
Disable Chrome Notifications using Selenium Python

Time:05-19

I'd like to stop all Chrome Browser notifications whenever I run a selenium python file.

Example of what I'm referring to:

enter image description here

I found the following code online that theoretically blocks all chrome browser notifications:

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

option = Options()
option.add_argument('--disable-notifications')
***driver = webdriver.Chrome(executable_path=ChromeDriverManager().install(), chrome_options=option)
driver.get("https://www.example.com")

However, whenever I run the code, I get the following error at the *** line:

enter image description here

Any help would be greatly appreciated. Thank you.

CodePudding user response:

To make disabled cookies/notifications/click accept, you can try as follows:

from selenium import webdriver
from selenium.webdriver.chrome.service import Service
from webdriver_manager.chrome import ChromeDriverManager
from selenium.webdriver.chrome.options import Options
option = webdriver.ChromeOptions()

#enabled/disabled cookies/notifications/click accept.
option.add_experimental_option("excludeSwitches", ["enable-automation"])
option.add_experimental_option('useAutomationExtension', False)
option.add_argument("--disable-infobars")
option.add_argument("start-maximized")
option.add_argument("--disable-extensions")

#chrome to stay open
option.add_experimental_option("detach", True)

# Pass the argument 1 to allow and 2 to block
option.add_experimental_option("prefs", { 
    "profile.default_content_setting_values.notifications": 2 
})

driver = webdriver.Chrome(service=Service(ChromeDriverManager().install()),options=option)
driver.get("https://www.example.com")
  • Related