使用java在selenium webdriver中等待子窗口

使用java在selenium webdriver中等待子窗口,java,selenium,selenium-webdriver,Java,Selenium,Selenium Webdriver,如何在selenium web驱动程序中等待子窗口 当我点击submit按钮时,它会引导我进入子窗口,我断言Url,但有时子窗口需要时间 当前正在使用thread.sleep(4000) 但这是错误的方法。 // parent window String subWindowHandler = null; Set<String> handles = driver.getWindowHandles(); // get all window // hand

如何在selenium web驱动程序中等待子窗口

当我点击submit按钮时,它会引导我进入子窗口,我断言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;

    }
//父窗口
字符串子窗口处理程序=null;
设置句柄=驱动程序。getWindowHandles();//打开所有窗口
//处理
迭代器迭代器=handles.Iterator();
while(iterator.hasNext()){
subWindowHandler=iterator.next();
}
driver.switchTo().window(子WindowHandler);//切换到弹出窗口
System.out.println(driver.getTitle());
字符串Current_Url=driver.getCurrentUrl();
System.out.println(“其他选项卡中当前打开的Url-->”+当前_Url);
试一试{
Assert.assertEquals(“http://intelliview-dev.psi.psigroupinc.com/Reports/ReportView.aspx“,当前Url);
logger.info(“通过断言”);
info(“用户可以登录到我的帐户应用程序”);
}捕获(断言错误){
logger.info(“断言失败”);
logger.info(“用户提供无效的用户名或密码”);
投掷e;
}

您可以使用
预期条件。numberOfWindowsToBe

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

按你的要求计算

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

您可以使用
ExpectedConditions.numberOfWindowsToBe

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

按你的要求计算

 new WebDriverWait(driver,30).until(ExpectedConditions.numberOfWindowsToBe(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);
       }
  }
然后使用切换到新窗口

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);
       }
  }
Set allWindows=driver.getWindowHandles();
迭代器ite=allWindows.Iterator();
while(ite.hasNext()){
字符串popupHandle=ite.next().toString();
如果(!popupHandle.contains(父窗口)){
driver.switchTo()窗口(popupHandle);
//在新窗口上执行操作
//然后切换回父窗口
driver.switchTo()窗口(父窗口);
}
}

为此,您需要确保应用程序打开新窗口所需的最长时间(可接受的时间限制)。 在此之前,请使用获取当前窗口的详细信息

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);
       }
  }
然后使用切换到新窗口

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);
       }
  }
Set allWindows=driver.getWindowHandles();
迭代器ite=allWindows.Iterator();
while(ite.hasNext()){
字符串popupHandle=ite.next().toString();
如果(!popupHandle.contains(父窗口)){
driver.switchTo()窗口(popupHandle);
//在新窗口上执行操作
//然后切换回父窗口
driver.switchTo()窗口(父窗口);
}
}

请不要发布已经提供的相同答案。请不要发布已经提供的相同答案。。