且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

如何在 Chrome 浏览器中禁用通知弹出窗口

更新时间:2022-05-09 01:26:51

由于您已将 ChromeOptions() 的实例创建为 chrome_options,您需要将其作为在调用 webdriver.Chrome() 时使配置生效的参数如下:

As you have created an instance of ChromeOptions() as chrome_options you need to pass it as an argument to put the configurations in effect while invoking webdriver.Chrome() as follows :

from selenium import webdriver
from selenium.webdriver.chrome.options import Options
import time

chrome_options = webdriver.ChromeOptions()
prefs = {"profile.default_content_setting_values.notifications" : 2}
chrome_options.add_experimental_option("prefs", prefs)
chrome_options.add_argument("start-maximized")
driver = webdriver.Chrome(chrome_options=chrome_options, executable_path=r'C:\path\to\chromedriver.exe')
driver.get("https://www.arttoframe.com/")
time.sleep(6)
driver.quit()

注意:

  • 要最大化 Chrome 浏览器而不是 maximize_window(),请使用 ChromeOptions() 类参数 start-maximized
  • 在您的程序结束时使用 driver.quit() 而不是 driver.close().
  • To maximize the Chrome browser instead of maximize_window() use the ChromeOptions() class argument start-maximized
  • At the end of your program instead of driver.close() use driver.quit().