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

时间:2018-04-03 09:32:51

标签: python selenium selenium-webdriver webdriver selenium-chromedriver

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

2 个答案:

答案 0 :(得分:1)

 LinearLayout layout = new LinearLayout(this);
 for(int i = 0;i <4;i++){
 Spinner spinner = new Spinner(this);
 layout.addView(spinner);
 setContentView(layout);
 }

这些可用于dismiss-right和accept-left

答案 1 :(得分:0)

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

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.close()使用 driver.quit()