如何使用Selenium for Firefox禁用推送通知?

我想通过Selenium Webdriver启动Firefox浏览器时禁用通知。
我找到了这个答案 ,但它已被弃用,并且在Firefox上不起作用(尽管它在Chrome上运行得很好)。

我将这个依赖项用于我的pom.xml

 org.seleniumhq.selenium selenium-java 3.11.0  

如果您的用是禁用通知,则以下是选项:

  • 要在Firefox浏览器客户端中禁用通知 ,请获取FirefoxProfile的帮助并传递Key dom.webnotifications.enabled ,其值为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); 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配置文件中的文档创建的。

  • 要在Chrome浏览器客户端中禁用通知 ,请借助setExperimentalOption()传递包含profile.default_content_setting_values.notifications值为 2HashMap

     System.setProperty("webdriver.chrome.driver", "C:\\path\\to\\chromedriver.exe"); Map prefs = new HashMap(); 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/");