Handle browser notifications in selenium [know your location]

Browser applications push notifications to user to know their location or push notification when user opens the application on browser.

Why notifications

These notifications are to engage users without knowing their contact details.

Like “know your location” helps to show near by details like weather, restaurants etc near to your area.

Push notifications help users to get desktop notification when there is a new update.

While we are automating the sites that request to allow or block notification, it’s annoying as sometimes you can’t get see the application until you respond to the notification requests.

we will see how we can handle these notifications [allow or block] using selenium

How to handle in selenium

we can use ChromeOptions for chrome browser & FirefoxOptions for firefox browser

For chrome browser

To disable “know your location” notification, use

option.addArguments("disable-geolocation");

To disable any other browser notifcation, use

option.addArguments("disable-notifications");

Let’s see implementation

For firefox browser

To disable “know your location” notification, use

option.addPreference("geo.enabled", false);

To disable any other browser notifcation, use

option.addPreference("dom.webnotifications.enabled", false);

Let’s see implementation

If find helpful, share with friends!

For more topics, refer selenium java tutorial

Content posted is based on learning or working experience,
Share the post with others if find useful,
Any queries?, leave comments or discuss on our facebook:qavalidation.com, Thanks!

Leave a Reply

Your email address will not be published. Required fields are marked *