如何使用Selenium for Firefox和Chrome禁用推送通知? [英] How to disable push-notifications using Selenium for Firefox and Chrome?

查看:109
本文介绍了如何使用Selenium for Firefox和Chrome禁用推送通知?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想通过Selenium Webdriver启动Firefox浏览器时禁用通知。

我发现这个答案,但它已被弃用,并不适用于我的Firefox(虽然它在Chrome上完美运行)。

I want to disable the notification when I launch Firefox browser through Selenium Webdriver.
I found this answer, but it's deprecated and does not work for me on Firefox (it works perfectly on Chrome though).

我对 pom.xml 使用此依赖项:

<dependency>
    <groupId>org.seleniumhq.selenium</groupId>
    <artifactId>selenium-java</artifactId>
    <version>3.11.0</version>
</dependency>


推荐答案

如果 usecase 是禁用通知以下是选项:

If your usecase is to disable the notification following are the options :

  • To disable Push Notification in Firefox browser client take help of a FirefoxProfile and pass the Keys dom.webnotifications.enabled and dom.push.enabled along with the desired Value as false :

System.setProperty("webdriver.gecko.driver", "C:\\path\\to\\geckodriver.exe");
ProfilesIni profile = new ProfilesIni();
FirefoxProfile testprofile = profile.getProfile("debanjan");
testprofile.setPreference("dom.webnotifications.enabled", false);
testprofile.setPreference("dom.push.enabled", false);
DesiredCapabilities dc = DesiredCapabilities.firefox();
dc.setCapability(FirefoxDriver.PROFILE, testprofile);
FirefoxOptions opt = new FirefoxOptions();
opt.merge(dc);
WebDriver driver =  new FirefoxDriver(opt);
driver.get("https://www.ndtv.com/");


注意:此方法使用存储在我的本地系统中的名称 debanjan 的现有 FirefoxProfile ,该文件是根据 在Windows上创建新的Firefox个人资料

Note : This method uses an existing FirefoxProfile by the name debanjan stored in my local system which was created following the documentation at Creating a new Firefox profile on Windows


  • 要在 Chrome 浏览器客户端中停用通知,请获取 setExperimentalOption()的帮助以传递 HashMap 包含 profile.default_content_setting_values.notifications 2

  • To disable notification in Chrome browser client take help of a setExperimentalOption() to pass a HashMap containing profile.default_content_setting_values.notifications with Value as 2 :

System.setProperty("webdriver.chrome.driver", "C:\\path\\to\\chromedriver.exe");
Map<String, Object> prefs = new HashMap<String, Object>();
prefs.put("profile.default_content_setting_values.notifications", 2);
prefs.put("credentials_enable_service", false);
prefs.put("profile.password_manager_enabled", false);
ChromeOptions options = new ChromeOptions();
options.setExperimentalOption("prefs", prefs);
options.addArguments("start-maximized");
options.addArguments("disable-infobars");
options.addArguments("--disable-extensions");
options.addArguments("--disable-notifications");
WebDriver driver = new ChromeDriver(options);
driver.get("https://www.ndtv.com/");


这篇关于如何使用Selenium for Firefox和Chrome禁用推送通知?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆