Grzegorz Szwed
Grzegorz Szwed

Reputation: 21

c# Selenium WebDriver - How to disable notifications on Facebook Page?

I am a beginner in Selenium WebDriver, and I have a problem with poping out notification on Facebook page while I'm trying to log in. I searched a lot, but i did not find anyhing usefull. I found code in java, convert into C# but it didn't work.(I hope that a did it properly) I tried something like this, but nothing. Please, help with this if you can.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using OpenQA.Selenium;
using OpenQA.Selenium.Chrome;


namespace ConsoleApp1
{
    class Program
    {
        static void Main(string[] args)
        {
            Dictionary<string, object> hash = new Dictionary<string, object>();
            hash.Add("profile.default_content_setting_values.notifications", 2);
            ChromeOptions op = new ChromeOptions();
            op.AddAdditionalCapability("hash",hash);
            IWebDriver driver = new ChromeDriver("path to googlewebdriver");

            driver.Url = "http://facebook.com";
            driver.Manage().Window.Maximize();

            driver.FindElement(By.Id("email")).SendKeys("my email");
            driver.FindElement(By.Id("pass")).SendKeys("mypassw" + Keys.Enter);
            driver.FindElement(By.XPath("//*[@id='content_container']")).Click();
        }
    }
}

Upvotes: 2

Views: 5287

Answers (3)

Sathish
Sathish

Reputation: 79

ChromeOptions op = new ChromeOptions();  
op.AddArguments("--disable-notifications");   

Most importantly need to check the versions of Chrome Driver. Some versions it may not support.

Upvotes: 0

gimlichael
gimlichael

Reputation: 1394

To disable the annoying "some-annoying-website wants to: 🔔 Show notifications", set your ChromeOptions like this:

ChromeOptions options = new ChromeOptions();
options.AddUserProfilePreference("profile.default_content_setting_values.notifications", 2);

And pass it to your ChromeDriver:

using (var chrome = new ChromeDriver(options)) ...

Upvotes: 1

Raj
Raj

Reputation: 674

If you simply want to disable "all" notification on chrome browser, you can use switch --disable-notifications

Code in C# to launch chrome with this switch:

 ChromeOptions options = new ChromeOptions();
 options.AddArguments("--disable-extensions"); // to disable extension
 options.AddArguments("--disable-notifications"); // to disable notification
 options.AddArguments("--disable-application-cache"); // to disable cache
 driver = new ChromeDriver(options);

Here's list of switches available for chrome browser: Chromium Comamnd Line Switches

Alternatively, you have options to handle alert by using this code statement:

options.UnhandledPromptBehavior = UnhandledPromptBehavior.Dismiss;

You have other options available here to accept, dismiss, ignore etc.

Upvotes: 4

Related Questions