如何使用 Selenium Webdriver 处理浏览器级通知?

问题描述 投票:0回答:6
我正在使用 Selenium Webdriver 和核心 Java 自动化一些测试用例。在 Chrome 浏览器中,单击按钮的一个测试用例中,我收到浏览器级通知“显示带有允许和阻止选项的通知”。 我想选择允许选项。有谁知道如何使用 Selenium webdriver 处理此类通知?

更多详情请参考以下截图

java selenium-webdriver webdriver
6个回答
5
投票
请按照以下步骤操作:

A) 使用 JAVA :

适用于旧 Chrome 版本 (<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);


对于新的 Chrome 版本(>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);


对于火狐浏览器:

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) 使用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- driver\chromedriver.exe') driver.get('https://www.facebook.com')

C) 使用 C#:

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

3
投票
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");
    

2
投票
处理此类需求的步骤:

    打开 Firefox 配置文件管理器(转到“开始”菜单,键入
  1. firefox.exe -p
     启动配置文件管理器)
  2. 创建一个新的配置文件(例如配置文件名称为
  3. customfirefox
  4. 导航至
  5. about:permissions
     
  6. 进行所需的配置并保存配置文件
现在使用新创建的配置文件通过调用 Firefox 来运行测试

ProfilesIni ffProfiles = new ProfilesIni(); FirefoxProfile profile = ffProfiles.getProfile("customfirefox"); WebDriver driver = new FirefoxDriver(profile);

这将始终使用 Firefox 以及保存在配置文件中的自定义配置。

希望这对你有用..!!!

祝你好运。


2
投票
您可以使用下面的代码来允许chrome发送通知:

ChromeOptions options=new ChromeOptions(); Map<String, Object> prefs=new HashMap<String,Object>(); prefs.put("profile.default_content_setting_values.notifications", 1); //1-Allow, 2-Block, 0-default options.setExperimentalOption("prefs",prefs); ChromeDriver driver=new ChromeDriver(options);

它非常适合我。如果它不适合您,请告诉我。 干杯!!


0
投票

-1
投票
更新: 请使用隐身模式以避免此类浏览器级通知和 cookie 问题。

© www.soinside.com 2019 - 2024. All rights reserved.