如何使用 Selenium for Firefox 和 Chrome 禁用推送通知? [英] How to disable push-notifications using Selenium for Firefox and 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 中使用此依赖项:
I'm use this dependency for my pom.xml:
<dependency>
<groupId>org.seleniumhq.selenium</groupId>
<artifactId>selenium-java</artifactId>
<version>3.11.0</version>
</dependency>
推荐答案
如果您的用例是禁用通知,以下是选项:
If your usecase is to disable the notification following are the options :
禁用推送通知 在 Firefox 浏览器客户端中使用 FirefoxProfile 并传递 Keys dom.webnotifications.enabled 和 dom.push.enabled 以及所需的 Value 为 false :
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,Value 为 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屋!