如何使用 Selenium Webdriver 在“显​​示通知"弹出窗口中单击“允许" [英] How to click Allow on Show Notifications popup using Selenium Webdriver

查看:36
本文介绍了如何使用 Selenium Webdriver 在“显​​示通知"弹出窗口中单击“允许"的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试登录 Facebook.成功登录后,我得到一个浏览器弹出窗口:

如何使用网络驱动程序单击允许并继续?

解决方案

请按照以下步骤操作:

A) 使用 Java:

<块引用>

对于旧 Chrome 版本 (<50):

//创建ChromeOptions类的实例ChromeOptions options = new ChromeOptions();//添加chrome开关禁用通知 - "**--disable-notifications**"options.addArguments("--disable-notifications");//设置驱动程序exe路径System.setProperty("webdriver.chrome.driver","path/to/driver/exe");//将 ChromeOptions 实例传递给 ChromeDriver 构造函数WebDriver 驱动程序 =new ChromeDriver(options);

<小时><块引用>

对于新的 Chrome 版本 (>50):

//创建一个映射来存储首选项映射<字符串,对象>prefs = new HashMap();//添加键和值映射如下以关闭浏览器通知//传递参数1允许和2阻止prefs.put("profile.default_content_setting_values.notifications", 2);//创建一个ChromeOptions实例ChromeOptions options = new ChromeOptions();//设置实验选项 - 首选项options.setExperimentalOption("prefs", prefs);//现在将 ChromeOptions 实例传递给 ChromeDriver 构造函数以初始化 chrome 驱动程序,这将在 chrome 浏览器上关闭此浏览器通知WebDriver driver = new ChromeDriver(options);

<小时><块引用>

对于火狐:

 WebDriver 驱动程序;FirefoxProfile 配置文件 = 新的 FirefoxProfile();profile.setPreference("permissions.default.desktop-notification", 1);DesiredCapabilities capabilities=DesiredCapabilities.firefox();Capability.setCapability(FirefoxDriver.PROFILE, profile);驱动程序 = 新的 FirefoxDriver(功能);driver.get("http://google.com");

<小时>

B) 使用 Python:

from selenium import webdriver从 selenium.webdriver.chrome.options 导入选项选项 = 选项()option.add_argument("--disable-infobars")option.add_argument("开始最大化")option.add_argument("--disable-extensions")# 传递参数 1 允许和 2 阻止option.add_experimental_option(首选项",{profile.default_content_setting_values.notifications":1})driver = webdriver.Chrome(chrome_options=option, executable_path='path-of-驱动程序chromedriver.exe')driver.get('https://www.facebook.com')

C) 使用 C#:

ChromeOptions options = new ChromeOptions();options.AddArguments("--disable-notifications");//禁用通知IWebDriver driver = new ChromeDriver(options);

I'm trying to login to Facebook. After a successful login, I get a browser popup:

How with the webdriver can I click Allow and proceed forward?

解决方案

Please Follow below steps :

A) USING JAVA :

For Old Chrome Version (<50):

//Create a instance of ChromeOptions class
ChromeOptions options = new ChromeOptions();

//Add chrome switch to disable notification - "**--disable-notifications**"
options.addArguments("--disable-notifications");

//Set path for driver exe 
System.setProperty("webdriver.chrome.driver","path/to/driver/exe");

//Pass ChromeOptions instance to ChromeDriver Constructor
WebDriver driver =new ChromeDriver(options);


For New Chrome Version (>50):

//Create a map to store  preferences 
Map<String, Object> prefs = new HashMap<String, Object>();

//add key and value to map as follow to switch off browser notification
//Pass the argument 1 to allow and 2 to block
prefs.put("profile.default_content_setting_values.notifications", 2);

//Create an instance of ChromeOptions 
ChromeOptions options = new ChromeOptions();

// set ExperimentalOption - prefs 
options.setExperimentalOption("prefs", prefs);

//Now Pass ChromeOptions instance to ChromeDriver Constructor to initialize chrome driver which will switch off this browser notification on the chrome browser
WebDriver driver = new ChromeDriver(options);


For Firefox :

    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("http://google.com");


B) USING PYTHON :

from selenium import webdriver
from selenium.webdriver.chrome.options import Options

option = Options()

option.add_argument("--disable-infobars")
option.add_argument("start-maximized")
option.add_argument("--disable-extensions")

# Pass the argument 1 to allow and 2 to block
option.add_experimental_option("prefs", { 
    "profile.default_content_setting_values.notifications": 1 
})

driver = webdriver.Chrome(chrome_options=option, executable_path='path-of- 
driverchromedriver.exe')
driver.get('https://www.facebook.com')

C) USING C#:

ChromeOptions options = new ChromeOptions();
options.AddArguments("--disable-notifications"); // to disable notification
IWebDriver driver = new ChromeDriver(options);

这篇关于如何使用 Selenium Webdriver 在“显​​示通知"弹出窗口中单击“允许"的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆