Reading list Switch to dark mode

    Handle Browser Level Notification Using Selenium

    While surfing the internet, everyone must have come across so many websites that send notifications. Those notifications can be on a 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 the Google Chrome browser:-

    web push notification

    On a side note, we would like to mention about WooCommerce Push Notification plugin that we have developed allowing merchants to send custom push notifications to subscribers.

    Step-by-step execution of Code:-

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

    1:  Create an instance of ChromeOptions class

    Start your headless eCommerce
    now.
    Read More
    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 on which we want to handle the notifications.

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

    Code:-

    The complete code will look like this:-

    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 🙂 Keep coming back up for more dev blogs.

    As an official WooCommerce plugin vendor, we have developed many different plugins to add more features, functionalities, and new integrations to the WooCommerce platform.

    One of the most popular plugins is WooCommerce Multi Vendor, which allows third-party sellers to sell their products on the same WordPress website platform.

    . . .
    Add a comment

    Leave a Comment

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


    63 comments

  • shivanshu
    Map prefs = new HashMap();
    prefs.put(“profile.default_content_setting_values.notifications”, 2);
    ChromeOptions opt= new ChromeOptions();
    opt.setExperimentalOption(“prefs”, prefs);
    WebDriver driver= new ChromeDriver(opt);
    driver.get(“https://www.redbus.in/”);

    i ahev use this code if above 50 is browser version but not working plz help

    • Praveen Pal (Moderator)
      Hello,
      you can try this one :

      Map prefs = new HashMap(); prefs.put(“profile.default_content_setting_values.notifications”, 2); ChromeOptions options = new ChromeOptions(); options.setExperimentalOption(“prefs”, prefs);
      WebDriver driver = new ChromeDriver(options);

      I have checked ,it is working fine for >50 browser .
      thanks,

  • shivanshu
    Hello mam

    i have written same code but unable to run code. plz help

    ChromeOptions opt= new ChromeOptions();
    opt.addArguments(“–disable-notifications”);
    System.setProperty(“webdriver.chrome.driver”,”C:\\Users\\Acer\\eclipse-workspace\\Selenium_Projects\\Chrome_driver\\chromedriver.exe”);

    WebDriver driver= new ChromeDriver(opt);
    driver.get(“https://www.cleartrip.com/”);

    • Praveen Pal (Moderator)
      Hello,

      Greetings for day!

      You can try this one:

      ChromeOptions options=new ChromeOptions();
      Map prefs=new HashMap();
      prefs.put(“profile.default_content_setting_values.notifications”, 1);
      options.setExperimentalOption(“prefs”,prefs);
      ChromeDriver driver=new ChromeDriver(options);

      thanks and regards,
      Webkul.

  • Vinay
    Can you please let me know how to do the same in Safari browser.
    • Praveen Pal (Moderator)
      Hello,
      we would like to inform you that you can handle notification in safari by using below code.You can try this mentioned below code.

      For running your script on safari browser you first need to add safari driver extension. you can dowload it from https://www.seleniumhq.org/download/ and then install it. Below is the sample code to use safari driver :-
      //Instantiate apple WebDriver
      WebDriver sDriver = new SafariDriver();
      //Use apple browser WebDriver to browse webpage https://webkul.com
      sDriver.get(” https://webkul.com“);
      //Find element on https://webkul.com
      WebElement store = sDriver.findElement(By.xpath(“/html/body/header/div/div[2]/nav[1]/div/div[2]/ul/li/a”));
      //Click the web element above.
      store.click();

  • seph
    Hi, how can I make this as a method and use when necessary? Thank you!
    • Priya Singh (Moderator)
      Hello,
      As per my understanding, you are trying to create a method for this operation and then call that method whenever required.

      Create a method under class scope.

      private static void notification() {

      your code
      }

      public static void main(String[] args) {

      //call your method wherever required
      notification();

      }

      • seph
        Thanks for your reply. Actually yes, but I couldn’t do it, I mean I am not sure where to put WebDriverManager. I want to call web browser pop-up blocker whenever needed without writing everything..
        This didn’t work.
        public static void notification() {
        WebDriverManager.chromedriver().setup();
        ChromeOptions options = new ChromeOptions();
        options.addArguments(“–disable-notifications”);
        WebDriver driver = new ChromeDriver(options);

        }
        }

        • Priya Singh (Moderator)
          Please share your full code.
  • Nischitha
    Hi, I have added the code as given above but I see that it is not updating the changes could u please help me with this
    • Priya Singh (Moderator)
      can you share the code and the scenario?
  • nischitha
    Hi, I need a help here.. I have used the same code, but I don’t see a difference. Could someone help me out with this
  • Aadhithyan
    Thanks. Very helpful.
    • Priya Singh (Moderator)
      Thanks for your feedback.
  • Gopinath
    Why should we disable notifications ?
  • thambi babu dasari
    I want to click on allow to accept the desktop notifications
    how can I do that using selenium web driver
    • Priya Singh (Moderator)
      Hello Thambi
      you can try the following code

      WebDriver driver ;
      FirefoxProfile profile = new FirefoxProfile();
      profile.setPreference(“permissions.default.desktop-notification”, 1);
      DesiredCapabilities capabilities=DesiredCapabilities.firefox();
      capabilities.setCapability(FirefoxDriver.PROFILE, profile);
      driver = new FirefoxDriver(capabilities);
      driver.get(“your Web site”);

      • thambi babu
        thank you..
        I want to handle it in chrome browser.
        I got a code to handle chrome notifications using chromeOptions.
        but, according to my scenario I need to make a voice call to other user in my application so once I logged in to app.. It allowing the notifications fine.. after that when I was click on voice call icon it asking the permission to allow or block microphone so how can handle this again for chrome browser. Is there any solution please let me know?
        thank you …
        • Priya Singh (Moderator)
          hello,
          you can try this one:

          ChromeOptions options=new ChromeOptions();
          Map prefs=new HashMap();
          prefs.put(“profile.default_content_setting_values.notifications”, 1);
          options.setExperimentalOption(“prefs”,prefs);
          ChromeDriver driver=new ChromeDriver(options);

          • surekha
            this one is not working
          • Priya Singh (Moderator)
            can you share the code and the scenario?
  • shaik ismail
    Thank you for your information it is very useful for me
    • Priya Singh (Moderator)
      Thanks for your feedback.
  • Amir Atias
    hi,
    can you please share node.js code for the same ??
  • Paras Verma
    if we want to allow the notifications, how we can do that.
    please help for this situation
    • Priya Singh (Moderator)
      WebDriver driver ;
      FirefoxProfile profile = new FirefoxProfile();
      profile.setPreference(“permissions.default.desktop-notification”, 1);
      DesiredCapabilities capabilities=DesiredCapabilities.firefox();
      capabilities.setCapability(FirefoxDriver.PROFILE, profile);
      driver = new FirefoxDriver(capabilities);
      driver.get(“your Web site”);
  • mahesh
    what about in chrome? i am unable to click on allow button for microphone access.
    • Paras Verma
      hi mahesh,
      try this one:

      ChromeOptions options=new ChromeOptions();
      Map prefs=new HashMap();
      prefs.put(“profile.default_content_setting_values.notifications”, 1);
      //1-Allow, 2-Block, 0-default
      options.setExperimentalOption(“prefs”,prefs);
      ChromeDriver driver=new ChromeDriver(options);

  • Ashwin Kumar
    can you please share python code for the same ??
    • Priya Singh (Moderator)
      Hello Ashwin,

      For Python you can try below code:

      chrome_options = webdriver.ChromeOptions()

      prefs = {“profile.default_content_setting_values.notifications” : 2}

      chrome_options.add_experimental_option(“prefs”,prefs)

      driver = webdriver.Chrome(chrome_options=chrome_options)

  • Qasim
    Thanks a lot. God bless u
    • Garima Pathak (Moderator)
      Thanks for your feedback 🙂
  • Dipali Shimpi
    Thankyou so much
    • Garima Pathak (Moderator)
      Thank you for the feedback 🙂
  • Hitendra S Deshmukh
    Thanks a lot.
    • Garima Pathak (Moderator)
      Thanks for your feedback 🙂
  • yogesh
    Hi yogesh here,
    The above solution runs fine for first page but notification again block the page in second page
    Eg. 1. home page applied code works fine
    2. after login notification blocks the app.
    i have applied
    options.addArguments(“–disable-notifications”);
    WebDriver driver1 =new ChromeDriver(options);
    after login page i can do one activity but it open another chrome browser as we have kept WebDriver driver1. all activity is done in same chrome instance but aother browser opens every time is my issue .
    can you please help me on this.
    thanks in advance
    • Garima Pathak (Moderator)
      can you share the code and the scenario?
  • Sowmiya
    Clearly explained. Thanks a lot 🙂
    • Garima Pathak (Moderator)
      Thank you for you feedback:)
  • kancha
    Hi Garima, Can you please help me out for the same popup in firefox browser?
    • Garima Pathak (Moderator)
      For firefox driver try with the below code:-
      System.setProperty(“webdriver.gecko.driver”,”Path of geckodriver”);
      FirefoxProfile profile = new FirefoxProfile();
      profile.setPreference(“dom.webnotifications.enabled”, false);
      WebDriver driver = new FirefoxDriver();
      driver.get(“http://wordpressdemo.webkul.com/wordpress-latest-tweets/”);
      driver.manage().window().maximize();
  • kancha
    its working fine..Thank you so much for the help:)
    • Garima Pathak (Moderator)
      Thanks for your feedback 🙂
  • Meet
    This code works fine when we have a notification as soon as webpage opens and we get notification. But, in case of webpage where notification appears at later stage, can you suggest what can be done. Above code doesn’t disable to notification at later stage.
    • Garima Pathak (Moderator)
      This code will work on later stage also you have to add this script with the scripts of previously automated pages.
  • Rushikesh
    Hi Garima,

    i am facing one chrome which appears after the application when i logout so how is this helpful for me…

    please advice…
    Thanks

  • Garima Pathak (Moderator)
    For running your script on safari browser you first need to add safari driver extension. you can dowload it from https://www.seleniumhq.org/download/ and then install it. Below is the sample code to use safari driver :-
    //Instantiate apple WebDriver
    WebDriver sDriver = new SafariDriver();
    //Use apple browser WebDriver to browse webpage https://webkul.com
    sDriver.get(” https://webkul.com“);
    //Find element on https://webkul.com
    WebElement store = sDriver.findElement(By.xpath(“/html/body/header/div/div[2]/nav[1]/div/div[2]/ul/li/a”));
    //Click the web element above.
    store.click();
  • Ana
    Hi, I want to Allow notifications, is that possible? I didn’t find the argument.
    • Garima Pathak (Moderator)
      This request is to disable notification.Right now there is no option to enable the same as above subject is not treated as a browser popup, it is treated as a browser notification.
      We disable the notifications so that the execution of rest code will continue without any hindrance with the notifications. If you have any query in executing please do let us know, we will surely come up with a solution.
      • Vikram Singh
        Hi Garima, tried using it but it didn’t help. I am using chrome 72 version. Not sure if you tested it on a different version of chrome. I am basically clicking on adobe flash icon which is showing up on my flash app during automation and clicking on it after which allow/block popup shows up.
  • Sakshi Nagpal
    How to do in mobile chrome browser
    • Garima Pathak (Moderator)
      For Mobile chrome browser or ios safari browser you can use Appium testing tool. We have to just write a normal webdriver test, and use selenium as server with special set of capabilities. We will surely explain more about Appium in our next blog.
  • Nirmal
    Thanks, really helpfull
  • sarika
    If I try using same code fro Chrome driver its not working.. Iam still able to see notification message
  • Ayush Bhatnagar
    how do I click on the Allow or Block buttons on this pop-up.
  • santosh
    what about firefox driver ?
    • Garima Pathak (Moderator)
      For firefox we just have to include below code:-
      System.setProperty(“webdriver.gecko.driver”,”Path of geckodriver”);
      FirefoxProfile profile = new FirefoxProfile();
      profile.setPreference(“dom.webnotifications.enabled”, false);
      WebDriver driver = new FirefoxDriver();
      driver.get(“http://wordpressdemo.webkul.com/wordpress-latest-tweets/”);
      driver.manage().window().maximize();
      • vrushali
        what is dom?
        • Garima Pathak (Moderator)
          DOM(Data Object Model) is a way by which HTML content is structured. In selenium as we locate elements by Id, CSS selectors, XPath and Link Text in the same way we can also locate page element By DOM also .
          • Rahul Shaw
            DOM stands for Document Object Model
  • samet
    thank 🙂
  • Back to Top
    It works now, very happy, Webkul is always willing to help wherever they need to, their customer service is out of this world.
    Alain Stout
    CEO
    www.Takoda.Shop
    Talk to Sales

    Global

    Live Chat

    Message Sent!

    If you have more details or questions, you can reply to the received confirmation email.

    Back to Home