7
WebDriverWait wait = new WebDriverWait(driver, 60)

WebElement element = driver.findElement(By.xpath("//div[contains(text(),'Loading...')]"));
System.out.println("Test");
wait.until(ExpectedConditions.not(ExpectedConditions.presenceOfElementLocated(By.xpath("//div[contains(text(),'Loading...')]"))));
System.out.println("Test");

ページの読み込みが完了するのを待っています。最初の「テスト」がコンソールに出力され、wait.until ステートメントの実行時に以下の例外が出力されます。ローディング画面が消えた後でも、wait.until はまだ待機中です。要素の Staleness も既に試しましたが、機能せず、同じタイムアウト例外が発生します。読み込みが完了すると、要素は DOM で使用できなくなります

4

5 に答える 5

11

presenceOfElementLocated使用する代わりに、要素が存在しないのを待ちたい場合presenceOfAllElementsLocatedBy:

wait.until(ExpectedConditions.not(ExpectedConditions.presenceOfAllElementsLocatedBy(By.xpath("//div[contains(text(),'Loading...')]"))));

ロケータに適合する要素がページ上になくなるまで待機します。

于 2015-06-05T10:05:41.840 に答える
4

最初のステートメントで要素が表示されるのを待っていません。つまり、

WebElement element = driver.findElement(By.xpath("//div[contains(text(),'Loading...')]"));

これが原因だと思いますNoSuchElementException...
次のことを試すことができます:

new WebDriverWait(driver,60).until(ExpectedConditions.visibilityOfElementLocated(By.xpath("//div[contains(text(),'Loading...')]")));

new WebDriverWait(driver,60).until(ExpectedConditions.invisibilityOfElementLocated(By.xpath("//div[contains(text(),'Loading...')]")));

上記のコードは、最初に要素の可視性を待ち、次に非可視性を待ちます。

于 2013-11-12T13:55:45.887 に答える
0

これを試すことができます:

new WebDriverWait(driver, TimeSpan.FromSeconds(10)).Until(d => d.FindElement(By.Id("searchTextBox0")).Displayed)
于 2017-08-17T13:18:19.843 に答える
0

ページがロードされるのを待つだけでよい場合は、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 を返し、 が aWebElementvisibilityOfElementLocated返すことに注意してくださいBoolean。したがって、 を使用して条件を連鎖させることはできません.andThen(Function)

于 2019-03-22T16:17:52.773 に答える