ページがロードされるのを待つだけでよい場合は、Javascript 関数を実行して、ページのロードが完了したことを確認できます。
String val = "";
do {
val = (String)((JavascriptExecutor)driver).executeScript("return document.readyState");
// DO WHATEVER
} while (!"complete".equals(val));
使用findElement()
する場合は、要素を見つけようとする前に暗黙の待機を使用する必要があります。それ以外のNoSuchElementException
場合、コンポーネントがページにロードされていない場合にスローされる可能性があります。
driver.manage().timeouts().implicitlyWait(5, TimeUnit.SECONDS); // Blocks NoSuchElementExceptions for 5 seconds when findElement() is called (set for the duration of driver's life.
WebElement element = driver.findElement(By.xpath("//div[contains(text(),'Loading...')]"));
この戦略は、テストのパフォーマンスに影響を与える可能性が高いため、慎重に使用する必要があります。WebDriverWait
または、 (明示的な待機)を使用する必要があります。
WebDriverWait wait = new WebDriverWait(driver, 60);
wait.until(ExpectedConditions.visibilityOfElementLocated(By.xpath("//div[contains(text(),'Loading...')]"))); // Presence of component checks the existence of the element in the DOM which it will always be true
System.out.println("Testing visibility passed...");
wait.until(ExpectedConditions.invisibilityOfElementLocated(By.xpath("//div[contains(text(),'Loading...')]"))); // Presence of component checks the existence of the element in the DOM which it will always be true
System.out.println("Testing invisibility passed...");
最後の戦略では、visibilityOfElementLocated
が a を返し、 が aWebElement
をvisibilityOfElementLocated
返すことに注意してくださいBoolean
。したがって、 を使用して条件を連鎖させることはできません.andThen(Function)
。