【问题标题】:wait for child window in selenium webdriver using java使用java在selenium webdriver中等待子窗口
【发布时间】:2019-10-09 12:38:30
【问题描述】:

如何在 selenium web driver 中等待子窗口

当我单击提交按钮时,它会引导我进入子窗口并声明 Url,但有时子窗口需要一些时间才能出现 当前使用 thread.sleep(4000) 但这是错误的方式。

    // parent window
    String subWindowHandler = null;

    Set<String> handles = driver.getWindowHandles(); // get all window
    // handles
    Iterator<String> iterator = handles.iterator();
    while (iterator.hasNext()) {
        subWindowHandler = iterator.next();
    }
    driver.switchTo().window(subWindowHandler); // switch to popup window
    System.out.println(driver.getTitle());

    String Current_Url = driver.getCurrentUrl();

    System.out.println("Current open Url in other Tab -->" + Current_Url);

    try {
        Assert.assertEquals("http://intelliview-dev.psi.psigroupinc.com/Reports/ReportView.aspx", Current_Url);
        logger.info("Assertion Passed");
        logger.info("User is able to login to My Account Application");
    } catch (AssertionError e) {
        logger.info("Assertion Failed");
        logger.info("User Provide Invalid Username or Password");
        throw e;

    }

【问题讨论】:

    标签: java selenium selenium-webdriver


    【解决方案1】:

    您可以使用ExpectedConditions.numberOfWindowsToBe

    单击调用窗口的事件,然后使用下面的代码,然后使用切换另一个窗口

    根据您的要求输入计数

     new WebDriverWait(driver,30).until(ExpectedConditions.numberOfWindowsToBe(2));
    

    【讨论】:

      【解决方案2】:

      为此,您需要确保应用程序打开新窗口所需的最长时间(可接受的时间限制)是多少。 在此之前使用

      获取当前窗口详细信息
      String parent_window = driver.getWindowHandle();
      

      然后等待新窗口使用

      new WebDriverWait(driver,AcceptableTimeLimit).until(ExpectedConditions.numberOfWindowsToBe(2));
      

      然后使用切换到新窗口

      Set<String> allWindows = driver.getWindowHandles();
      Iterator ite=allWindows.iterator();
      while(ite.hasNext()){
          String popupHandle=ite.next().toString();
          if(!popupHandle.contains(parent_window)){
              driver.switchTo().window(popupHandle);
              //perform your operations on New window
              //Then switch back to parent window
              driver.switchTo().window(parent_window);
             }
        }
      

      【讨论】:

      • 请不要发布已经提供的相同答案..
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2016-12-25
      • 1970-01-01
      • 2013-12-31
      • 1970-01-01
      • 2013-05-26
      • 2018-01-07
      • 2014-04-04
      相关资源
      最近更新 更多