每當我運行 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")
但是,每當我運行代碼時,都會在 *** 行收到以下錯誤:

任何幫助將不勝感激。謝謝你。
uj5u.com熱心網友回復:
要使禁用的cookies/通知/單擊接受,您可以嘗試如下:
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")
轉載請註明出處,本文鏈接:https://www.uj5u.com/qukuanlian/477081.html
下一篇:進入新頁面前需要登錄
