我遇到了Selenium WebDriver在仅执行一次后退出for循环的问题。大概是获取内容和加载页面之前开始循环的问题。有没有可能让webdriver等待页面加载?
List<WebElement> albums = new ArrayList<WebElement>();
albums = driver.findElements(By.className("album_title"));
for (WebElement we : albums) {
we.click();
if (driver.findElement(By.id("delete_album_prompt")).isDisplayed()) {
driver.findElement(By.id("delete_album_prompt")).click();
driver.findElement(By.id("delete_album_yes")).click();
} else {
break;
}
}发布于 2012-12-18 22:36:00
您可以使用Thread.sleep(20000);在循环开始之前使用此方法。
发布于 2012-12-18 20:59:33
您需要等待一个元素被加载。
在Selenium documentation中,它们提供了以下示例,该示例将等待谷歌页面加载。在本例中,它最多等待10秒,等待title元素加载并具有指定值。
// Google's search is rendered dynamically with JavaScript.
// Wait for the page to load, timeout after 10 seconds
(new WebDriverWait(driver, 10)).until(new ExpectedCondition<Boolean>() {
public Boolean apply(WebDriver d) {
return d.getTitle().toLowerCase().startsWith("cheese!");
}
});发布于 2013-06-12 13:29:41
使用此选项,其中TimeOut被定义为int值。
Wait wait = DriverManager.getWait(Driver.TIMEOUT);
wait.until(ExpectedConditions.visibilityOfElementLocated(by));基本上是等待一个元素被加载。这比提供睡眠要好。
https://stackoverflow.com/questions/13933359
复制相似问题