硒更新的Chrome无法禁用浏览器通知(尝试了其他解决方案) [英] Selenium newer Chrome cannot disable browser notification (Tried other solns)

查看:287
本文介绍了硒更新的Chrome无法禁用浏览器通知(尝试了其他解决方案)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I know this is an old question, and I've tried answers from a few posts such as Disable Chrome notifications (Selenium)

很遗憾,这些方法都无效,浏览器通知弹出窗口仍然出现,并中断了我的模拟.

Unfortunately none worked, the browser notification popup still comes and interrupts my simulations.

我的Chrome版本是75.0.3770.100(官方内部版本)(64位),可在MacOS上运行.

My Chrome version is 75.0.3770.100 (Official Build) (64-bit), running on MacOS.

此问题被标记为

After this question was marked as a duplicate of How to disable push-notifications using Selenium for Firefox and Chrome?, I've tried the solutions, but it still did not work for me.

            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);


下面是我尝试过的原始解决方案.


Below are the original solutions I tried.

        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);

但是通知仍然出现在窗口左上方的"xxxxwebsite希望显示通知允许阻止"中.

But the notification still comes "xxxxwebsite wants to show notifications Allow Block" on the upper left of the window.

我做错了什么?

推荐答案

我刚刚在Windows 75上针对网站

I just tested this on Windows with Chrome 75.0.3770.142 (64 bit) and ChromeDriver 75.0.3770.90 (see log below) against web site https://www.nzz.ch (because it asks for permission to show push notifications). I used Selenium 3.14.0.

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

对我来说,这里也提到的开关和链接资源中的开关可靠地禁止了弹出窗口的显示:

For me the switch also mentioned here and in the linked resources reliably deactivates the popup from being displayed:

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

在我的情况下,无需更改默认设置profile.default_content_setting_values.notifications.因此,如果这对您不起作用

Changing the default setting profile.default_content_setting_values.notifications is not necessary in my case. So if this does not work for you

  • 您未正确设置选项
  • 您使用的(可能是过时的)ChromeDriver版本不支持该版本,或者
  • 由于某些原因在MacOS上驱动程序未实现.

P.S .:对我来说,无论是Firefox还是Chrome浏览器,例如单击链接并导航到其他页面,即使在显示通知弹出窗口期间也是如此.您在那里有任何问题吗?还是弹出窗口只是一个烦人?

P.S.: In both Firefox and Chrome for me it was not problem to e.g. click links and navigate to other pages even during the notification pop-up was shown. Did you have any problems there or is the pop-up just an annoyance?

这篇关于硒更新的Chrome无法禁用浏览器通知(尝试了其他解决方案)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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