【问题标题】:How to disable notification popup in Chrome Browser如何在 Chrome 浏览器中禁用通知弹出窗口
【发布时间】:2018-09-12 13:56:54
【问题描述】:
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

【问题讨论】:

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


    【解决方案1】:

    由于您已将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.quit() 代替 driver.close()

    【讨论】:

    • 非常感谢你因为有你我现在很开心,再次感谢你
    【解决方案2】:
    driver.switch_to_alert().dismiss()
    driver.switch_to_alert().accept()
    

    这些可以使用dismiss-right和accept-left

    【讨论】:

    • 您好!虽然这段代码可以解决问题,including an explanation 解决问题的方式和原因确实有助于提高帖子的质量,并可能导致更多的赞成票。请记住,您正在为将来的读者回答问题,而不仅仅是现在提问的人。请edit您的回答添加解释并说明适用的限制和假设。
    猜你喜欢
    • 1970-01-01
    • 2019-02-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-01-22
    • 2022-12-16
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多