Home > OS >  How To Accept Chrome Notifications Selenium
How To Accept Chrome Notifications Selenium

Time:04-29

Notification Example

I have been all over stack overflow, but all the selenium stuff from 2018 is not working for me, can someone give me an update on how to change cookies/disable notifications/click accept.

None of the three things I'm doing right now are working

import selenium
import random
import keyboard
from selenium.webdriver.common.by import By
from selenium.webdriver.common.keys import Keys
from selenium import webdriver
from selenium.webdriver.chrome.service import Service as ChromeService
from webdriver_manager.chrome import ChromeDriverManager
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC

service = ChromeService(executable_path=ChromeDriverManager().install())

option=webdriver.ChromeOptions()
option.add_experimental_option("excludeSwitches", ["enable-automation"])
option.add_experimental_option('useAutomationExtension', False)
option.add_argument("--disable-infobars")
option.add_argument("--disable-extensions")

driver = webdriver.Chrome(service=service)


driver.set_window_size(1920, 1080)



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

I think I'm missing something with the webdriver/Chrome(service=service) bit or something. I can't find anything in the documentation.

CodePudding user response:

You're setting the "option.add_experimental..." preference after you've created the driver. Do that before. Also be sure to include the options in your new driver call.

service = ChromeService(executable_path=ChromeDriverManager().install())
option=webdriver.ChromeOptions()
option.add_experimental_option("excludeSwitches", ["enable-automation"])
option.add_experimental_option('useAutomationExtension', False)
option.add_argument("--disable-infobars")
option.add_argument("--disable-extensions")
# 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, options=option)


driver.set_window_size(1920, 1080)
  • Related