问题
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()
Console Logs :
C:\Users\Dell\PycharmProjects\untitled\venv\Scripts\python.exe
C:/Users/Dell/PycharmProjects/untitled/newaaa.py
Process finished with exit code 0
回答1:
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()
Note :
- To maximize the Chrome browser instead of
maximize_window()use theChromeOptions()class argument start-maximized - At the end of your program instead of
driver.close()use driver.quit().
来源:https://stackoverflow.com/questions/49626707/how-to-disable-notification-popup-in-chrome-browser