Java 如何等待第二个窗口完全加载

Java 如何等待第二个窗口完全加载,java,selenium,selenium-webdriver,Java,Selenium,Selenium Webdriver,我从父窗口单击一个按钮,它在新(子)窗口中加载一个查看器,然后在第二个窗口中执行一些操作。在加载第二个窗口之前,脚本尝试执行一些操作,但失败了。如果我将设为Thred.sleep它将成功运行。但我不想使用thread.sleep。我们是否可以等待第二个/子窗口完全加载其页面。这里我的第二个窗口(浏览器)是一个PDF格式的查看器。下面是我试过的代码 WebElement row = ele.findElement(By.cssSelector("tr[data-ri=\"0\"]")); row.

我从父窗口单击一个按钮,它在新(子)窗口中加载一个查看器,然后在第二个窗口中执行一些操作。在加载第二个窗口之前,脚本尝试执行一些操作,但失败了。如果我将
设为Thred.sleep
它将成功运行。但我不想使用thread.sleep。我们是否可以等待第二个/子窗口完全加载其页面。这里我的第二个窗口(浏览器)是一个PDF格式的查看器。下面是我试过的代码

WebElement row = ele.findElement(By.cssSelector("tr[data-ri=\"0\"]"));
row.findElement(By.className("ui-selection-column")).click();
browser.findElement(By.id("frmResults:btnViewer")).click();

Thread.sleep(5000);

Set<String> AllWindowHandles = browser.getWindowHandles();
String window1 = (String) AllWindowHandles.toArray()[0];
scenario.write("Currently in Parent Window = "+ AllWindowHandles.toArray()[0]);
scenario.write(browser.getCurrentUrl());
scenario.write(browser.getTitle());

String window2 = (String) AllWindowHandles.toArray()[1]; // out of bounds error thrown here

scenario.write("Switching to Child (Viewer) window = "+ AllWindowHandles.toArray()[1]);
browser.switchTo().window(window2);
scenario.write(browser.getCurrentUrl());
scenario.write(browser.getTitle());
WebElement viewer = browser.findElement(By.id("outerDiv"));
assertThat(viewer.isDisplayed()).isTrue();
//browser.close();

scenario.write("Again Switching back to Parent window = "+ AllWindowHandles.toArray()[0]);
browser.switchTo().window(window1);
scenario.write(browser.getCurrentUrl());
scenario.write(browser.getTitle());
错误:

java.lang.ArrayIndexOutOfBoundsException: 1
        at Steps.Steps.verify_the_test_records_are_displayed_in_the_results_table_in_Search_Results_page(Steps.java:115)
        at ?.Then Verify the test records are displayed in the results table in Search Results page(Test.feature:10)

[ERROR] Tests run: 1, Failures: 1, Errors: 0, Skipped: 0, Time elapsed: 30.552 s <<< FAILURE! - in Runner.TestRunner
[ERROR] feature(Runner.TestRunner)  Time elapsed: 29.389 s  <<< FAILURE!
cucumber.runtime.CucumberException: java.lang.ArrayIndexOutOfBoundsException: 1
Caused by: java.lang.ArrayIndexOutOfBoundsException: 1
java.lang.ArrayIndexOutOfBoundsException:1
在Steps.Steps.verify\u中,在搜索结果页面(Steps.java:115)中的结果表中显示测试记录
在?处。然后验证测试记录是否显示在搜索结果页面的结果表中(测试功能:10)

[错误]测试运行:1,失败:1,错误:0,跳过:0,经过的时间:30.552秒而不是
thread.sleep()
,您可以使用
ExpectedConditions
类在以下位置添加目标显式等待:


ExpectedConditions
应该在调用
String window2=(String)AllWindowHandles.toArray()[1]之前等待两个窗口存在--这应该确保
浏览器。getWindowHandles()
在尝试切换到第二个窗口之前返回
2

是的,我得到了这个逻辑,并且我知道它。但我所犯的错误却摆在它面前。我已经用错误更新了我的问题,以及我得到错误的那一行。请调查一下并帮助我。@mmar我想我现在明白你的问题了
ExpectedConditions
实现了一个
numberOfWindowsToBe
方法,在调用
switchTo().window(window2)
之前,可以等待窗口数为2,以避免越界异常。Yes。当我输出
scenario.write(String.valueOf(AllWindowHandles.size())
AllWindowHandles
的大小为“1”。这很有道理,我现在明白你的意思了。我已使用备用解决方案更新了示例,以等待2个窗口存在。是。你成功了。现在很好用。我将此显式等待放在这一行之前
Set AllWindowHandles=browser.getWindowHandles()java.lang.ArrayIndexOutOfBoundsException: 1
        at Steps.Steps.verify_the_test_records_are_displayed_in_the_results_table_in_Search_Results_page(Steps.java:115)
        at ?.Then Verify the test records are displayed in the results table in Search Results page(Test.feature:10)

[ERROR] Tests run: 1, Failures: 1, Errors: 0, Skipped: 0, Time elapsed: 30.552 s <<< FAILURE! - in Runner.TestRunner
[ERROR] feature(Runner.TestRunner)  Time elapsed: 29.389 s  <<< FAILURE!
cucumber.runtime.CucumberException: java.lang.ArrayIndexOutOfBoundsException: 1
Caused by: java.lang.ArrayIndexOutOfBoundsException: 1
import org.openqa.selenium.support.ui.WebDriverWait;
import static org.openqa.selenium.support.ui.ExpectedConditions.numberOfWindowsToBe;
// we need the above using statements to use WebDriverWait and ExpectedConditions

// first wait for number of windows to be 2:
WebDriverWait wait = new WebDriverWait(browser, 10);
wait.until(ExpectedConditions.numberOfWindowsToBe(2)));

// switch to a new window
String window2 = (String) AllWindowHandles.toArray()[1];
scenario.write("Switching to Child (Viewer) window = "+ AllWindowHandles.toArray()[1]);
browser.switchTo().window(window2);

// wait on some element on the page to be fully loaded
wait.until(ExpectedConditions.visibilityOfElementLocated(By.id("outerDiv")));