gpt4 book ai didi

java - WebDriver 退出

转载 作者:行者123 更新时间:2023-12-02 07:23:06 24 4
gpt4 key购买 nike

我遇到 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;
}

}

最佳答案

您需要等待元素加载。

Selenium documentation ,他们提供了以下示例,该示例将等待谷歌页面加载。在本例中,它最多等待 10 秒,等待标题元素加载并具有指定值。

// 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!");
}
});

关于java - WebDriver 退出,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13933359/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com