我想在通过 Selenium Webdriver 启动 Firefox 浏览器时禁用通知。
我发现this answer ,但它已被弃用,并且在 Firefox 上对我不起作用(但它在 Chrome 上完美工作)。
我将此依赖项用于我的 pom.xml:
<dependency>
<groupId>org.seleniumhq.selenium</groupId>
<artifactId>selenium-java</artifactId>
<version>3.11.0</version>
</dependency>
最佳答案
如果您的用例是禁用通知以下选项:
要在 Firefox 浏览器客户端中禁用 Push Notification,请借助 FirefoxProfile 并传递 key dom.webnotifications。 enabled 和 dom.push.enabled 以及所需的 Value 为 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
,该配置文件是按照 Creating a new Firefox profile on Windows 处的文档创建的
要在 Chrome 浏览器客户端中禁用通知,请借助setExperimentalOption()来传递HashMap 包含 profile.default_content_setting_values.notifications,值 为 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/");
关于java - 如何使用 Selenium 为 Firefox 和 Chrome 禁用推送通知?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59455249/