Select Page

Handle Browser Level Notification Using Selenium

Garima Pathak
Published: February 3, 2023

While surfing internet, everyone must have came across so many websites who sends notifications. Those notifications can be on desktop or device even when that web page is not open in our browser. Generally, we call them Web push notifications. Below is the screenshot of notifications from chrome browser:-

Step by step execution of Code:-

We have to follow below mentioned steps to handle these notifications :-

1:  Create a instance of ChromeOptions class

ChromeOptions options = new ChromeOptions();

2: Add chrome switch to disable notification – “–disable-notifications”

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

3: Set path for the chrome driver

System.setProperty("webdriver.chrome.driver", "/home/users/garima.pathak/Desktop/softwares/chromedriver");

4: Pass ChromeOptions instance to ChromeDriver Constructor

WebDriver driver =new ChromeDriver(options);

5: Give the navigation of the page in which we want to handle the notifications.

driver.get("http://wordpressdemo.webkul.com/wordpress-latest-tweets/");

Code:-

The complete code will look like :-

package automationFramework;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.chrome.ChromeOptions;
public class Notifications {
public static void main(String[] args) {
ChromeOptions options = new ChromeOptions();
options.addArguments("--disable-notifications");
System.setProperty("webdriver.chrome.driver", "/home/users/garima.pathak/Desktop/softwares/chromedriver");
WebDriver driver =new ChromeDriver(options);
driver.get("http://wordpressdemo.webkul.com/wordpress-latest-tweets/");
driver.manage().window().maximize();
}
}

That’s all about handling the notifications using selenium.

Thanks for reading this blog 🙂

Source: webkul.com