Java 禁用 Chrome 通知 (Selenium)

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/34343423/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-11 15:33:30  来源:igfitidea点击:

Disable Chrome notifications (Selenium)

javagoogle-chromeseleniumselenium-chromedriver

提问by fabio_vac

I just want to disable Chrome notifications in the Chrome opened by a Selenium Java application. (using java code)

我只想在 Selenium Java 应用程序打开的 Chrome 中禁用 Chrome 通知。(使用java代码)

Notifications like this one:

类似这样的通知:

enter image description here

在此处输入图片说明

The problem is that settings manually set are lost after browser's window is closed.

问题是手动设置的设置在浏览器窗口关闭后丢失。

采纳答案by fabio_vac

This question was answered in the: "chromedriver-users" google forum. This is the working answer:

这个问题在“chromedriver-users”谷歌论坛中得到了回答。这是工作答案:

Map<String, Object> prefs = new HashMap<String, Object>();
prefs.put("profile.default_content_setting_values.notifications", 2);
ChromeOptions options = new ChromeOptions();
options.setExperimentalOption("prefs", prefs);
WebDriver driver = new ChromeDriver(options);

回答by wailord

you can use:

您可以使用:

chrome_options = Options()
chrome_options.add_argument("--disable-notifications")
browser = webdriver.Chrome(chrome_options=chrome_options)

回答by user1063865

            ChromeOptions ops = new ChromeOptions();
            ops.addArguments("--disable-notifications");
            System.setProperty("webdriver.chrome.driver", "./lib/chromedriver");
            driver = new ChromeDriver(ops);

回答by Mesut GUNES

Someone needs this for Capybara or Watir, you can pass the --disable-notificationsas an argument like "--start-fullscreen", "--disable-infobars". The following workes:

有人需要这样的水豚或的Watir,你可以通过--disable-notifications像一个参数"--start-fullscreen", "--disable-infobars"。以下工作:

Capybara.register_driver :chrome do |app|
  args = ["--disable-notifications"]
  Capybara::Selenium::Driver.new(app, {:browser => :chrome, :args => args})
end

回答by Ravi Kulkarni

public class MultipleWindowHandle
{
    public static void main(String[] args)
    {
        System.setProperty("webdriver.chrome.driver", "E:\NEWSEL\chromedriver.exe");

        // Create object of HashMap Class as shown below.
        Map<String, Object> prefs = new HashMap<String, Object>();

        // Set the notification setting it will override the default setting.
        prefs.put("profile.default_content_setting_values.notifications", 2);

        // Create object of ChromeOption class.
        ChromeOptions Roptions = new ChromeOptions();

        // Set the experimental option.
        Roptions.setExperimentalOption("prefs", prefs);

        // Open chrome browser.
        ChromeDriver driver = new ChromeDriver(Roptions);
        driver.get("https://my.monsterindia.com/login.html");

        Set<String> id = driver.getWindowHandles();
        Object[] data = id.toArray();
        driver.switchTo().window((String)data[1]); driver.close();
    }
}