isDisplayed() はあまり良いアプローチではないようです。待機からいくつかのアイデアを得る: 明示的および暗黙的な待機メカニズム:
Explicit wait
WebDriverWait.until(condition-that-finds-the-element)
Implicit wait
driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
Explicit Waits:
WebDriver driver = new FirefoxDriver();
driver.get("http://somedomain/url_that_delays_loading");
WebElement myDynamicElement = (new WebDriverWait(driver, 10))
.until(new ExpectedCondition<WebElement>(){
@Override
public WebElement apply(WebDriver d) {
return d.findElement(By.id("myDynamicElement"));
}});
Implicit Waits:
WebDriver driver = new FirefoxDriver();
driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
driver.get("http://somedomain/url_that_delays_loading");
WebElement myDynamicElement = driver.findElement(By.id("myDynamicElement"));
The example what you have given both do exact same thing..
In Explicit wait, WebDriver evaluates the condition every 500
milliseconds by default ..if it is true, it comes out of loop
Where as in ImplicitWait WebDriver polls the DOM every 500
milliseconds to see if element is present..
Difference is
1. Obvious - Implicit wait time is applied to all elements in your
script but Explicit only for particular element
2. In Explicit you can configure, how frequently (instead of 500
millisecond) you want to check condition.
3. In Explicit you can also configure to ignore other exceptions than
"NoSuchElement" till timeout..
ここでさらに情報を得ることができます
また、要素がページにレンダリングされるのを待つために流暢な待機メカニズムを使用します。実際には、css セレクターまたは xpath を関数に渡し、単純に Web 要素を取得します。
public WebElement fluentWait(final By locator){
Wait<WebDriver> wait = new FluentWait<WebDriver>(driver)
.withTimeout(30, TimeUnit.SECONDS)
.pollingEvery(5, TimeUnit.SECONDS)
.ignoring(NoSuchElementException.class);
WebElement foo = wait.until(
new Function<WebDriver, WebElement>() {
public WebElement apply(WebDriver driver) {
return driver.findElement(locator);
}
}
);
return foo; } ;
流暢な待機の説明
これが明らかになることを願っています)