gpt4 book ai didi

java - 使用 Selenium 循环遍历 List

转载 作者:行者123 更新时间:2023-11-30 06:58:17 27 4
gpt4 key购买 nike

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

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