硒较新的Chrome无法禁用浏览器通知(尝试其他解决方案)



我知道这是一个老问题,我已经尝试了一些帖子的答案,例如禁用Chrome通知(Selenium(

不幸的是,没有一个有效,浏览器通知弹出窗口仍然出现并中断我的模拟。

我的Chrome版本是75.0.3770.100(官方版本((64位(,在MacOS上运行。


编辑:

在这个问题被标记为如何禁用Selenium for Firefox和Chrome的推送通知?的副本后,我已经尝试了解决方案,但它仍然对我不起作用。

            String chromePath = "somepath/selenium-2.48.2/chromedriver";
            System.setProperty("webdriver.chrome.driver", chromePath);

            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");
            driver = new ChromeDriver(options);

以下是我尝试过的原始解决方案。

        String chromePath = "somepathto/chromedriver";
        ChromeOptions chromeOptions = new ChromeOptions();
        chromeOptions.addArguments("--disable-notifications");
        System.setProperty("webdriver.chrome.driver", chromePath);
        WebDriver driver = new ChromeDriver(chromeOptions);
        // Login
        try {
            driver.get(sometestURL);
            driver.manage().window().maximize();    
            // do some operations
        } catch (Exception ex) {
            System.out.println(ex);
        }

我也试过这个:

        String chromePath = "somepath/selenium-2.48.2/chromedriver";
        Map<String, Object> prefs = new HashMap<String, Object>();
        prefs.put("profile.default_content_setting_values.notifications", 2);
        ChromeOptions chromeOptions = new ChromeOptions();
        chromeOptions.setExperimentalOption("prefs", prefs);
        System.setProperty("webdriver.chrome.driver", chromePath);
        WebDriver driver = new ChromeDriver(chromeOptions);

但是通知仍然出现在窗口左上角的"xxxx网站想要显示通知允许阻止"。

我做得不对吗?

我刚刚在Windows上使用Chrome 75.0.3770.142(64位(和ChromeDriver 75.0.3770.90(见下面的日志(针对网站 https://www.nzz.ch 对此进行了测试(因为它要求显示推送通知的权限(。我使用了硒 3.14.0。

08:27:37.184 [main] INFO  i.g.bonigarcia.wdm.WebDriverManager - Exporting webdriver.chrome.driver as C:Usersxxxxx.m2repositorywebdriverchromedriverwin3275.0.3770.90chromedriver.exe
Starting ChromeDriver 75.0.3770.90 (a6dcaf7e3ec6f70a194cc25e8149475c6590e025-refs/branch-heads/3770@{#1003}) on port 32864

对我来说,此处和链接资源中提到的开关可靠地停用了弹出窗口的显示:

options.addArguments("--disable-notifications");

就我而言,不需要更改默认设置profile.default_content_setting_values.notifications。因此,如果这对您不起作用

  • 要么您没有正确设置选项,
  • 您正在使用不支持它的(可能已过时的(ChromeDriver 版本,或者
  • 由于某种原因,在MacOS上驱动程序没有实现它。

PS:对我来说,在Firefox和Chrome中,即使在显示通知弹出窗口期间,单击链接并导航到其他页面也不是问题。您在那里有任何问题还是弹出窗口只是烦恼?

最新更新