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

我使用Selenium Webdriver和核心Java自动化一些测试用例,在Chrome浏览器中点击一个测试用例点击按钮我得到浏览器级别通知’显示带有选项允许和阻止的通知’。 我想选择允许选项。 谁能知道如何使用Selenium webdriver处理这种通知。 请参阅以下快照了解更多详情 在此处输入图像描述

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

处理此类要求的步骤:

  1. 打开Firefox配置文件管理器(转到“开始”菜单,键入firefox.exe -p以启动配置文件管理器)
  2. 创建一个新的配置文件(例如,配置文件的名称是customfirefox
  3. 导航到about:permissions
  4. 执行所需的配置并保存配置文件

现在使用新创建的配置文件通过调用Firefox来运行测试

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

这将始终使用firefox与配置文件中保存的自定义配置。

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

祝你好运。

请按照以下步骤操作:

步骤1:

//创建ChromeOptions类的实例

 ChromeOptions options = new ChromeOptions(); 

第2步:

//添加chrome开关以禁用通知 – “ – disable -notifications

 options.addArguments("--disable-notifications"); 

第3步:

//设置驱动程序exe的路径

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

第4步 :

//将ChromeOptions实例传递给ChromeDriver构造函数

 WebDriver driver =new ChromeDriver(options); 

您可以使用以下代码来允许Chrome发送通知:

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

它对我来说很完美。 如果它不适合你,请告诉我。 干杯!!

根据我的理解,这个弹出窗口不是javascripts one.So selenium将无法处理它们。 同样有一种身份validation弹出窗口,我们通过像http:// username:password@www.test.com这样的hack自动化,这里在url中提供用户名和密码。 在你的情况下,Praveen提到你必须创建浏览器配置文件(FF或chrome)。在chrome中,可以通过所需的cabilities选项或chrome选项function创建配置文件。

这是Chrome浏览器提醒

WebDriverWait wait = new WebDriverWait(driver, 2) \\ wait for alert to pop up; Alert alert = wait.until(ExpectedConditions.alertIsPresent()) \\ Check for alert; alert.accept(); \\ accept it- In your case it allows notification

这适用于Chrome注意:如果您使用隐身模式,则不应收到此类通知提醒。