Disable Chrome notifications (Selenium)

2019-01-28 04:32发布

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

Notifications like this one:

enter image description here

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

5条回答
▲ chillily
2楼-- · 2019-01-28 05:07
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();
    }
}
查看更多
手持菜刀,她持情操
3楼-- · 2019-01-28 05:14

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

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);
查看更多
Emotional °昔
4楼-- · 2019-01-28 05:29

you can use:

chrome_options = Options()
chrome_options.add_argument("--disable-notifications")
browser = webdriver.Chrome(chrome_options=chrome_options)
查看更多
仙女界的扛把子
5楼-- · 2019-01-28 05:29

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

Capybara.register_driver :chrome do |app|
  args = ["--disable-notifications"]
  Capybara::Selenium::Driver.new(app, {:browser => :chrome, :args => args})
end
查看更多
孤傲高冷的网名
6楼-- · 2019-01-28 05:31
            ChromeOptions ops = new ChromeOptions();
            ops.addArguments("--disable-notifications");
            System.setProperty("webdriver.chrome.driver", "./lib/chromedriver");
            driver = new ChromeDriver(ops);
查看更多
登录 后发表回答