java - 使用 Selenium 循环遍历 List<WebElement>

标签 java selenium selenium-webdriver selenium-chromedriver

我使用下面的代码获取 anchor 列表,然后我想要转到每个链接。我想出了下面的代码,但是在第一个循环之后我得到了以下异常

org.openqa.selenium.StaleElementReferenceException: stale element reference: element is not attached to the page document (Session info: chrome=55.0.2883.87)

List<WebElement> listingAnchorList = driver.findElements(By.xpath("//div[contains(@class,'cat')]/a"));

for (WebElement listingAnchor : listingAnchorList) {
    driver.get(listingAnchor.getAttribute("href"));
    System.out.println(driver.getTitle());
}

是否有办法做到这一点而不必每次都返回页面?

最佳答案

您可以在一些新的List中收集您的href属性,然后迭代它并打开每个页面。

关于java - 使用 Selenium 循环遍历 List<WebElement>,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41406015/

相关文章:

c# - API 测试的测试自动化

java - 使用 "ITestListener"或 "IReporter"监听器生成范围报告

java - Spring Cloud Stream Kinesis Binder - 并发

java - Acegi/Spring 安全支持 getUserPrincipal() 吗?

python - 为什么 socket 会干扰 Selenium ?

java selenium webdriver - 如何输出 javascript 对象的属性?

javascript - Selenium Webdriver 无法在 Electron 构建中工作

java - HTTP 状态 500 - 请求处理失败。嵌套异常是java.lang.NullPointerException

java - HashMap的HashMap->输出问题

python - 使用 selenium (Python) 轮换服务器 IP