How to Handle Wants to Show Notifications Message using Selenium

This post is intended to help automation enthusiasts on How to Handle Wants to Show Notifications message using Selenium. This is one of the most typical cases you could come across while trying to automate a website. This notification thingy is basically difficult to handle since it is a browser-based thing and not something that you can inspect.

To those who still don’t understand, here’s one example to show how this notification shows up with its allow and block options:

allow and block notification in Facebook

Remember it is also not an alert and hence using driver.switchTo().alert().dismiss() is not going to work here.

So how do we handle it?

In order to handle websites that want to show you the Show Notifications box, the best way to handle it is by using the ChromeOptions feature.

Chrome Options class has plenty of methods to handle all the features that are browser related. We will see a step by step tutorial on How to Handle Wants to Show Notifications message.

notification meme

Example of Facebook’s Wants to Show Notifications

I am going to show you how to handle this notification on Facebook which happens to send this message everytime you to open it.

So the code I was running dealt with simply logging in to Facebook. The notification was immediately flung. Here’s the code so far.

System.setProperty("webdriver.chrome.driver", System.getProperty("user.dir") + "\\drivers\\chromedriver.exe");
driver = new ChromeDriver();
loginToFacebook();

The third line is a method where I am basically using driver.get() to open Facebook and then logging in using my Username and Password then clicking on Login button.

When I run the above, I get the following notification that also darkens the page background:

facebook notifications How to Handle Websites with Wants to Show Notifications example

It makes it difficult to work on the rest of the page since the notification restricts you from accessing anything inside.

Steps on How to Handle Wants to Show Notifications Message

Just follow the following steps to understand how notifications should be handled in Selenium.

Step 1: Create an instance of the Class ChromeOptions.

 ChromeOptions options = new ChromeOptions();

Step 2: Use the addArguments method with the parameter “–disable-notifications” to read it. This should be your next line.

options.addArguments("--disable-notifications");

Step 3: Now use the instance of ChromeOptions as a parameter to ChromeDriver like this:

driver = new ChromeDriver(options);

In short, just replace the earlier mentioned code with:

System.setProperty("webdriver.chrome.driver", System.getProperty("user.dir") + "\\drivers\\chromedriver.exe")
ChromeOptions options = new ChromeOptions();
options.addArguments("--disable-notifications");
driver = new ChromeDriver(options);
loginToFacebook();

That should do it!

Now run your code. It will no longer pop up that filthy notification.

NOTE: If you are using Firefox instead of Chrome, you can handle it using FirefoxProfile class’ getProfile method.

Liked this tutorial on how to handle wants to show notifications message using Selenium? Check out more Selenium related tutorials on our website.

Scottshak

Poet. Author. Blogger. Screenwriter. Director. Editor. Software Engineer. Author of "Songs of a Ruin" and proud owner of four websites and two production houses. Also, one of the geekiest Test Automation Engineers based in Ahmedabad.

You may also like...

Leave a Reply