Search code examples
javaseleniumgoogle-chromechrome-options

How to click "Allow" automatically on Google Chrome pop-up using Selenium + Java


Based on what I have read, there is a way to do this for Google Chrome versions < 50 and a way to do this for Google Chrome versions > 50. I am using Google Chrome 91.

There is an answer on to this located here: How to click Allow on Show Notifications popup using Selenium Webdriver which states that I need to do something like this:

//Create a map to store  preferences 
Map<String, Object> prefs = new HashMap<String, Object>();

//add key and value to map as follow to switch off browser notification
//Pass the argument 1 to allow and 2 to block
prefs.put("profile.default_content_setting_values.notifications", 2);

//Create an instance of ChromeOptions 
ChromeOptions options = new ChromeOptions();

// set ExperimentalOption - prefs 
options.setExperimentalOption("prefs", prefs);

//Now Pass ChromeOptions instance to ChromeDriver Constructor to initialize chrome driver which will switch off this browser notification on the chrome browser
WebDriver driver = new ChromeDriver(options);

However, this does not work for me. This is what the pop up looks like pop-up

and this is how I am using it:

// Create a map to store preferences (to disable pop-up notifications)
Map<String, Object> prefs = new HashMap<String, Object>();

// add key and value to map as follow to switch off browser notification
// Pass the argument 1 to allow and 2 to block
prefs.put("profile.default_content_setting_values.notifications", 2);

// for local automated testing
this.chromeOptions = new ChromeOptions();
chromeOptions.setExperimentalOption("prefs", prefs);
chromeOptions.addArguments("--disable-notifications");
chromeOptions.addArguments("start-maximized");
String chromeDriverPath = "resources/chromedriver-91.exe";
System.setProperty("webdriver.chrome.driver", chromeDriverPath);
this.driver = new ChromeDriver(chromeOptions);
System.out.println("new chrome driver started.....");
this.driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);

So as you can see, I have tried multiple different ways. "--disable-notifications" did not work and neither did chromeOptions.setExperimentalOption("prefs", prefs); When I run my program, the pop-up is still there, I need to Selenium to click "Allow" on the pop-up so I can continue with the rest of the program.


Solution

  • You are trying to download a file, which is different behavior than showing notifications. Try

    prefs.put("profile.default_content_setting_values.automatic_downloads", 1);