如何在selenium Java中单击弹出窗口

时间:2017-11-17 03:36:03

标签: selenium-webdriver

enter image description here我是selenium的新手,并尝试使用包含注册表单的应用程序(ABC项目)自动化(在Java for Window和使用Chrome驱动程序中使用selenium Webdriver)。

完成表格并点击注册按钮后,我得到一个带有关闭(X)&的弹出信息消息(模态)。 OK Button&消息中的标题:在ABC项目下显示以下内容:通知用户注册成功的文本。

我已经尝试了几种方法来点击此弹出窗口中的“确定”按钮,但没有成功:

1. 

    Alert alert = driver.switchTo().alert();
                   alert.accept();

 2. 

    driver.findElement(By.xpath("//input[@value = 'alert']")).click();
               Alert javascriptAlert = myTestDriver.switchTo().alert();
               System.out.println(javascriptAlert.getText()); // Get text on alert box
               javascriptAlert.accept();
               ----> in this case I get only the text of the opened tab (from registration window: driver.getTitle();:ABC Project) but not the text of teh Info message(to see in logger.info) 

 3. 

    String winHandleBefore =   
        driver.getWindowHandle();
               driver.findElement(By.xpath("//div[@class='col-md-4']/button[1]")).click();
        // Xpath of register Button
               Set handles = driver.getWindowHandles();    ..... --->>, In this case, I don't get any Window Handler of new window the window handler from old and new are the same

其他提示:

  • 我也无法点击Window中的其他任何地方跳过弹出窗口
  • 在Google Developer工具中,我没有看到任何源代码,也没有弹出窗口中的任何元素(当弹出窗口出现时。
  • 我从开发人员那里听说这个窗口是一个javascript,但我没有 在源代码中看到任何东西(它也不适用于解决方案b 以上)

我感谢任何提示和支持 感谢

1 个答案:

答案 0 :(得分:3)

此图片看起来像是一个javascript警报。

所以下面的代码应该有效。

Alert alert = driver.switchTo().alert();
alert.accept();

也许这是一个等待问题。试试WebDriverWait

Alert alert = new WebDriverWait(driver, 20).until(ExpectedConditions.alertIsPresent());
alert.accept();
相关问题