禁用通知标志在两种浏览器上均不起作用

时间:2019-05-16 19:21:40

标签: python selenium selenium-chromedriver selenium-firefoxdriver

我正在尝试通过硒来运行一个网站,但是“想要:显示通知”的通知不断弹出并阻止脚本点击。 Notification message

我知道我需要给浏览器禁用它们所必需的参数,但是它们没有通过。我的问题同时提到了Chrome和Firefox,因为我不在乎哪个最终可以运行。如果您能帮助我让其中一位工作,我会很高兴。

我正在使用python 3.6.5;硒3.141; chrome版本74.0.3729.157(正式版本)(64位); firefox量子66.0.5(64位); geckodriver v0.24.0 Windows 64位; ChromeDriver 74.0.3729.6 Windows 64位。所有必需的文件都在PATH中。硒脚本可以完美地在两个Web浏览器上与网站进行交互。

我尝试将配置文件包含已保存的浏览器参数,但浏览器无法正确加载配置文件。我尝试给浏览器驱动程序options参数加上相关的标志,但没有用(代码部分中的更多内容)。

这是我现在的Chrome设置:

options = webdriver.ChromeOptions()
options.add_extension("uBlock.crx")  # loads uBlock origin extension, succesful
options.add_argument("--incognito")  # works, results in session being incognito
options.add_argument("--disable-notifications")  # still shows the notifications
driver = webdriver.Chrome(chrome_options=options)  # launch the session

这将产生一个由硒控制的chrome会话,通过该会话,脚本的其余部分将在页面上成功执行操作。

我还尝试了以下同样无效的代码段:

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

对于Firefox,这是我尝试过的最新操作,但也无效:

options = webdriver.FirefoxOptions()
options.set_preference("dom.webnotifications.enabled", False)
options.set_preference("dom.push.enabled", False)
driver = webdriver.Firefox(firefox_options=options)
driver.install_addon("C:\\Users\\ChowderII\\AppData\\Roaming\\Mozilla\\Firefox\\Profiles\\ik9e2pqc.selenium\\extensions\\uBlock0@raymondhill.net.xpi")  # successfully loads uBlock extension

这将产生一个由selenium控制的Firefox会话,通过该会话,脚本的其余部分将在页面上成功执行操作。

我尝试将标志传递为options对象和profile对象,没有区别。

我希望通知弹出窗口不会显示,但仍然会显示。

感谢任何人都可以帮助我!

0 个答案:

没有答案