3

すべてがイベントに保存されるプロジェクトに取り組んでいるため、サーバーが新しいデータに応答するのに時間がかかりました。私は ajax を使用してページを待機する Fluent を使用していますが、これは ajax を使用していません。そのため、再度更新しない場合は、新しいアイテムがあるかどうかを確認してページを更新したいと思います。これはSelenium 2でどのように達成されますか?

これは私がしました :

        def accountsAfterCreating = 0
        while (accountsAfterCreating <= existingAccounts) {
            driver.navigate().refresh()
            WebElement table = wait.until(new Function<WebDriver, WebElement>() {
                public WebElement apply(WebDriver driver) {
                    return driver.findElement(By.className("table"))
                }
            })
            accountsAfterCreating = table.findElements(By.className("amount")).size()
        }

正しい方法ですか?

4

3 に答える 3

2

このように明示的な待機を使用します try catch ブロックで

試す{

WebElement myDynamicElement = (new WebDriverWait(driver, 10))
  .until(ExpectedConditions.presenceOfElementLocated(By.id("myDynamicElement")));

}

catch()

{

driver.navigate().refresh()

}

于 2013-07-08T09:50:55.417 に答える
2

私は通常、このメソッドを使用して html タグを待ちます。待ち時間のご指定も承ります。

public boolean waitForElement(WebElement ele, String xpath, int seconds) throws InterruptedException{
    //returns true if the element appears within the time
    //false when timed out
    int t=0;
    while(t<seconds*10){
        if(ele.findElements(By.xpath(xpath)).size()>0)
            return true;
        else{
            Thread.sleep(100);
            t++;
            continue;
        }
    }       
    System.out.println("waited for "+seconds+"seconds. But couldn't find "+xpath+ " in the element specified");
    return false;
}
于 2013-07-08T10:04:17.243 に答える
0

このような答えにたどり着きました。クロージャーを使用しているため、これはgroovyでのみ機能します

    private boolean refreshUntil(Closure<Boolean> condition) {
    Wait<WebDriver> wait = new FluentWait<WebDriver>(driver)
        .withTimeout(8, TimeUnit.SECONDS)
        .pollingEvery(200, TimeUnit.MILLISECONDS)
        .ignoring(NoSuchElementException)
    wait.until(new Predicate<WebDriver>() {
        boolean apply(WebDriver driver) {
            driver.navigate().refresh()
            if (condition()) {
                return true
            }
            return false
        }
    })
    return true
}

そしてこのメ​​ソッドを呼び出す

refreshUntil {
            accountsBeforeCreation + 1 == driver.findElements(By.tagName("tr"))).size()
        }
于 2013-07-15T08:02:21.883 に答える