Skip to main content
 首页 » 编程设计

c# Selenium WebDriver之如何禁用 Facebook 页面上的通知

2024年02月25日55dyllove98

我是 Selenium WebDriver 的初学者,我在尝试登录时在 Facebook 页面上弹出通知时遇到问题。我搜索了很多,但没有找到任何有用的东西。我在 java 中找到了代码,将其转换为 C#,但它没有用。(我希望它做得正确)我尝试过类似的方法,但没有成功。如果可以,请帮忙解决这个问题。

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(); 
        } 
    } 
} 

请您参考如下方法:

如果你只是想在 chrome 浏览器上禁用“所有”通知,你可以使用开关 --disable-notifications

使用此开关启动 chrome 的 C# 代码:

 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); 

这是可用于 Chrome 浏览器的开关列表:Chromium Comamnd Line Switches

或者,您可以选择使用以下代码语句来处理警报:

options.UnhandledPromptBehavior = UnhandledPromptBehavior.Dismiss; 

您可以在此处使用其他选项来接受、拒绝、忽略等。