我使用下面的代码获取 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/