使用 Selenium Python 禁用 Chrome 通知

Disable Chrome Notifications using Selenium Python

每当我 运行 selenium python 文件时,我想停止所有 Chrome 浏览器通知。

我所指的示例:

我在网上找到了以下代码,理论上可以阻止所有 chrome 浏览器通知:

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")

但是,每当我 运行 代码时,我都会在 *** 行收到以下错误:

如有任何帮助,我们将不胜感激。谢谢。

要让disabled cookies/notifications/click接受,你可以尝试如下:

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")