简体   繁体   English

如何接受 Chrome 通知 Selenium

[英]How To Accept Chrome Notifications Selenium

Notification Example通知示例

I have been all over stack overflow, but all the selenium stuff from 2018 is not working for me, can someone give me an update on how to change cookies/disable notifications/click accept.我一直在堆栈溢出,但 2018 年的所有 selenium 东西对我不起作用,有人可以给我更新如何更改 cookies/禁用通知/单击接受。

None of the three things I'm doing right now are working我现在正在做的三件事都没有用

import selenium
import random
import keyboard
from selenium.webdriver.common.by import By
from selenium.webdriver.common.keys import Keys
from selenium import webdriver
from selenium.webdriver.chrome.service import Service as ChromeService
from webdriver_manager.chrome import ChromeDriverManager
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC

service = ChromeService(executable_path=ChromeDriverManager().install())

option=webdriver.ChromeOptions()
option.add_experimental_option("excludeSwitches", ["enable-automation"])
option.add_experimental_option('useAutomationExtension', False)
option.add_argument("--disable-infobars")
option.add_argument("--disable-extensions")

driver = webdriver.Chrome(service=service)


driver.set_window_size(1920, 1080)



# Pass the argument 1 to allow and 2 to block
option.add_experimental_option("prefs", { 
"profile.default_content_setting_values.notifications": 2 
})

I think I'm missing something with the webdriver/Chrome(service=service) bit or something.我想我遗漏了 webdriver/Chrome(service=service) 位之类的东西。 I can't find anything in the documentation.我在文档中找不到任何内容。

You're setting the "option.add_experimental..." preference after you've created the driver.您在创建驱动程序后设置“option.add_experimental ...”首选项。 Do that before.以前那样做。 Also be sure to include the options in your new driver call.还要确保在您的新驱动程序调用中包含这些选项。

service = ChromeService(executable_path=ChromeDriverManager().install())
option=webdriver.ChromeOptions()
option.add_experimental_option("excludeSwitches", ["enable-automation"])
option.add_experimental_option('useAutomationExtension', False)
option.add_argument("--disable-infobars")
option.add_argument("--disable-extensions")
# Pass the argument 1 to allow and 2 to block
option.add_experimental_option("prefs", { 
"profile.default_content_setting_values.notifications": 2 
})

driver = webdriver.Chrome(service=service, options=option)


driver.set_window_size(1920, 1080)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM