我想用selenium打開一個網站,並通過xpath提取一些文本。它正在工作,但只有在等待幾秒鐘後才能看到文本,直到整個網站加載完畢,但是檢查網站是否完全加載會更好。我想我應該檢查任何後臺連接(ajax調用,GET,POST或其他),最好的方法是什麼?目前,我試圖提取while循環文本(醜陋的解決方案):java:睡眠直到網站完全加載
WebDriver driver = new FirefoxDriver();
driver.get("http://www.website.com");
// Try to get text
while (true) {
try {
WebElement findElement = driver.findElement(By.xpath("expression-here"));
System.out.println(findElement.getText());
break;
// If there is no text sleep one second and try again
} catch (org.openqa.selenium.NoSuchElementException e) {
System.out.println("Waiting...");
Thread.sleep(1000);
}
}
你會如何解決這個問題?
已經定義了很多預期條件。請參閱ExpectedCondition.visibilityOfElementLocated – YoelBen