1

I want to deactivate this chrome alert (see attached) for my selenium script. What I tried so far:

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

Also tried with the arguments: disable-infobars and disable-popup-blocking. None of these solved the issue for me.

I tried the solution explained here: How to disable a How to disable a "Reload site? Changes you made may not be saved" popup for (python) selenium tests in chrome? But it doesn't work for me.

Sorry for the german alert, it asks if I want to leave the page. Chrome alert

timr2001
  • 21
  • 1
  • 5
  • Could you please translate what is the alert message says? – seenukarthi Aug 15 '19 at 08:17
  • 1
    Leave website? Your changes perhaps couldn't be saved . – timr2001 Aug 15 '19 at 08:21
  • But the alert message itself is not important. I want to start chrome, so that no alert will pop up. Doesnt matter on the message. – timr2001 Aug 15 '19 at 08:22
  • In ChromeDriver v75 and above, you can automatically dismiss or accept all prompts by setting, options.setUnhandledPromptBehaviour(UnexpectedAlertBehaviour.DISMISS); (or UnexpectedAlertBehaviour.ACCEPT). (This will occur before certain subsequent actions...in this case, you are navigating away so it should happen immediately) If you want to remove the alert entirely, you could modify the DOM to remove the "onbeforeunload" event handler by executing javascript. I don't know if there is a driver option to not show alerts. – pcalkins Aug 15 '19 at 18:15
  • Yes this works - but I want to remove it entirely without doing changes in the DOM. Is there any solution? – timr2001 Aug 16 '19 at 09:04

2 Answers2

0

For New Chrome Version (>50) you have to use the following,

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

So simply if you need to disable notifications you can use this statement,

profile.default_content_setting_values.notifications = 2

And to enable,

profile.default_content_setting_values.notifications = 1
Hasitha Jayawardana
  • 2,326
  • 4
  • 18
  • 36
  • Nope, doesn't work in this case. I assume that this is not a notification. Should be an "alert" or something else. – timr2001 Aug 16 '19 at 09:04
-1

have you tried giving navigation of a page of which you want to handle notifications of.

driver.get