【问题标题】:how to wait for the second window to load completely如何等待第二个窗口完全加载
【发布时间】:2019-11-22 19:52:32
【问题描述】:

我从父窗口单击一个按钮,它会在新(子)窗口中加载一个查看器,然后我在第二个窗口中执行一些操作。在加载我的第二个窗口本身之前,脚本会尝试执行一些操作,但它失败了。如果我输入Thred.sleep,它就会成功。但我不想使用thread.sleep。我们有什么办法可以等待 SECOND/CHILD 窗口完全加载其页面。这里我的第二个窗口(浏览器)是一种 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());

更新

当我尝试在这一行获取子窗口编号时出现错误。

String window2 = (String) AllWindowHandles.toArray()[1];

错误:

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 selenium selenium-webdriver


    【解决方案1】:

    除了thread.sleep(),您可以使用ExpectedConditions 类在number of expected windows 上添加有针对性的显式等待:

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

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

    【讨论】:

    • 是的,我明白这个逻辑并且我知道。但是我自己得到的错误。我已经用错误和我得到的行更新了我的问题。请调查一下并帮助我。
    • @mmar 我想我现在明白你的问题了。 ExpectedConditions实现了一个numberOfWindowsToBe方法,可以等待窗口数为2后再调用switchTo().window(window2),避免越界异常。
    • 是的。当我输出scenario.write(String.valueOf(AllWindowHandles.size())); 时,AllWindowHandles 的大小为“1”。
    • 有道理,我现在明白你的意思了。我已经用另一种解决方案更新了我的示例,以等待 2 个窗口存在。
    • 是的。你做到了。它现在工作正常。我将此显式等待放在此行Set&lt;String&gt; AllWindowHandles = browser.getWindowHandles(); 之前,它会等待并获取所有预期的窗口,然后再进行下一个操作,并且能够等待、加载并在新窗口上执行操作。非常感谢。我也接受了你的回答。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2011-12-23
    • 2019-12-19
    • 2013-08-31
    • 2021-02-05
    • 2013-10-31
    • 2019-02-10
    • 1970-01-01
    相关资源
    最近更新 更多