10

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.

fabio_vac
  • 584
  • 1
  • 6
  • 14
  • 1
    I don't understand why someone clicked down on this question! I think they don't understood the questione itself... – fabio_vac Dec 18 '15 at 08:46

5 Answers5

21

you can use:

chrome_options = Options()
chrome_options.add_argument("--disable-notifications")
browser = webdriver.Chrome(chrome_options=chrome_options)
wailord
  • 377
  • 3
  • 5
  • 3
    Please provide mor information, not only code, like how does this work or why this is better than the existing solution – Florian Koch Aug 11 '16 at 13:52
20

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);
fabio_vac
  • 584
  • 1
  • 6
  • 14
5
            ChromeOptions ops = new ChromeOptions();
            ops.addArguments("--disable-notifications");
            System.setProperty("webdriver.chrome.driver", "./lib/chromedriver");
            driver = new ChromeDriver(ops);
Vikas Garg
  • 119
  • 2
  • 4
  • 2
    This works best by passing an argument --disable-notifications to the application as of Chrome version 74 – jnoreiga May 15 '19 at 14:16
3

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
Mesut GUNES
  • 7,089
  • 2
  • 32
  • 49
0
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();
    }
}
L_J
  • 2,351
  • 10
  • 23
  • 28
  • 2
    While this code may answer the question, providing information on how and why it solves the problem improves its long-term value – L_J Jul 22 '18 at 10:28