如何在 Chrome 浏览器中禁用通知弹出窗口 [英] How to disable notification popup in Chrome Browser

查看:62
本文介绍了如何在 Chrome 浏览器中禁用通知弹出窗口的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

from selenium import webdriver
from selenium.webdriver.common.keys import Keys
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)
driver = webdriver.Chrome("C:\\chromedriver\\chromedriver.exe")
driver.maximize_window()
driver.get("https://www.arttoframe.com/")
time.sleep(6)
driver.close()

控制台日志:

C:\Users\Dell\PycharmProjects\untitled\venv\Scripts\python.exe 
C:/Users/Dell/PycharmProjects/untitled/newaaa.py

Process finished with exit code 0

推荐答案

由于您已将 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().

这篇关于如何在 Chrome 浏览器中禁用通知弹出窗口的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆