FluentWaitクラスを使用してみてください。
// Waiting 30 seconds for an element to be present on the page, checking
// for its presence once every 5 seconds.
Wait<WebDriver> wait = new FluentWait<WebDriver>(driver)
.withTimeout(30, SECONDS)
.pollingEvery(5, SECONDS)
.ignoring(NoSuchElementException.class);
WebElement foo = wait.until(new Function<WebDriver, WebElement>() {
public WebElement apply(WebDriver driver) {
return driver.findElement(By.id("foo"));
}
});
そのようにして、Selenium は特定の要素がロードされるまで一定時間待機します。
これで問題が解決し、ページを更新する必要がなくなることを願っています。
編集:
MrTi so friendly が指摘しているように、上記のコードはページを更新しません。特定の要素がロードされるまで、特定の期間だけ待機します。ページを更新しなくても、問題が解決するかもしれないと思っただけです。それでも問題が解決せず、ページを更新する必要がある場合はdriver.navigate().refresh()
、戻る前に次のように追加する必要があります。
// Waiting 30 seconds for an element to be present on the page, checking
// for its presence once every 5 seconds.
Wait<WebDriver> wait = new FluentWait<WebDriver>(driver)
.withTimeout(30, SECONDS)
.pollingEvery(5, SECONDS)
.ignoring(NoSuchElementException.class);
WebElement foo = wait.until(new Function<WebDriver, WebElement>() {
public WebElement apply(WebDriver driver) {
driver.navigate().refresh()
return driver.findElement(By.id("foo"));
}
});