繁体   English   中英

在 Python + Selenium Chrome WebDriver 中处理通知

[英]Handle notifications in Python + Selenium Chrome WebDriver

如何在 Python 中处理 Selenium Chrome WebDriver 通知?

已尝试解除/接受alertactive element但似乎必须以其他方式处理通知。 此外,所有 Google 搜索结果都促使我使用我并不真正需要的 Java 解决方案。 我是 Python 的新手。

提前致谢。

在此处输入图片说明

您可以使用 chrome 选项禁用浏览器通知。

chrome_options = webdriver.ChromeOptions()
prefs = {"profile.default_content_setting_values.notifications" : 2}
chrome_options.add_experimental_option("prefs",prefs)
driver = webdriver.Chrome(chrome_options=chrome_options)

按照以下代码使用 Python selenium 处理通知设置

from selenium import webdriver


from selenium.webdriver.chrome.options import Options

option = Options()

option.add_argument("--disable-infobars")

option.add_argument("start-maximized")

option.add_argument("--disable-extensions")

option.add_experimental_option("prefs", 
{"profile.default_content_setting_values.notifications": 2 
 }) 

注 1-允许通知,2-阻止通知

上面 16 年 12 月的答案在 2020 年 8 月对我不起作用。我相信 Selenium 和 Chrome 都发生了变化。

我正在使用 Chrome 84 的二进制文件,它被列为当前版本,尽管selenium.dev提供了 Chrome 85 的二进制文件。 我必须假设这是一个测试版,因为我没有其他信息。

到目前为止,我只有部分答案,但我相信以下两行是正确的方向。 他们是我今晚得到的。 我有一个向 Facebook 打开的测试窗口,并在问题中显示了确切的窗口。 上面 pythonjar 于 2016 年 12 月 30 日的回答中给出的代码为我生成了错误消息。 这些代码行没有错误,所以我相信这是正确的轨道。

>>> from selenium.webdriver import ChromeOptions
>>> chrome_options = ChromeOptions()

明天我会更新这个,如果我有时间处理它。

对不起,部分答案。 我希望尽快完成。 我希望这有帮助。 如果你先到了,请告诉我。

使用 Chrome Versão 85.0.4183.83 (Versão oficial) 64 位和 ChromeDriver 85.0.4183.83

这是代码,它正在工作

from selenium import webdriver

chrome_options = webdriver.ChromeOptions()
prefs = {"profile.default_content_setting_values.notifications" : 2}
chrome_options.add_experimental_option("prefs",prefs)
driver = webdriver.Chrome(chrome_options=chrome_options)
driver.get("https://www.google.com/")

2020/08/27

如果您使用的是ChromeDriver Version: 96python 代码结构将如下所示来处理 ChromeDriver 通知:

# Importing webdriver
from selenium import webdriver

# Importing webdriver Options to control notifications
from selenium.webdriver.chrome.options import Options

# Creating Instance
option = Options()

# Working with the 'add_argument' Method to modify Driver Default Notification
option.add_argument('--disable-notifications')

# Passing Driver path alongside with Driver modified Options
driver = webdriver.Chrome(executable_path= your_chrome_driver_path, chrome_options= option)

# You may pass the desired URL to create your Bot
driver.get('https://www.selenium.dev/documentation/webdriver/browser/')

# You may do your stuff now

# Let's not forget Quitting the Driver
driver.quit()

暂无
暂无

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

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