How to handle chrome notification in Selenium?

SeleniumAutomation TestingTesting Tools

We can handle Chrome notification in Selenium webdriver. These are generally known as the web push notifications and can be handled with the help of the ChromeOptions class. The below image shows a notification from the browser Chrome −

We have to create an object of this ChromeOptions class and apply the addArguments method on it. The parameter --disable-notifications is passed as a parameter to this method. Finally, this information is sent to the ChromeDriver.

Syntax

ChromeOptions o = new ChromeOptions();
o.addArguments("--disable-notifications");

Example

import org.openqa.selenium.By;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.chrome.ChromeOptions;
import org.openqa.selenium.WebDriver;
public class NotificationChrome {
   public static void main(String[] args) throws IOException {
      //object of ChromeOptions
      ChromeOptions o = new ChromeOptions();

      //set browser options o.addArguments("--disable-notifications");
      System.setProperty("webdriver.chrome.driver",
         "C:\\Users\\ghs6kor\\Desktop\\Java\\chromedriver.exe");
      // pass browser option to webdriver
      WebDriver driver = new ChromeDriver(o);
      //implicit wait
      driver.manage().timeouts().implicitlyWait(5, TimeUnit.SECONDS);
      //URL launch
      driver.get("https://www.justdial.com/Bangalore/Bakeries");
   }
}

Output

raja
Published on 08-Apr-2021 07:50:50
Advertisements