我想搜索亚马逊上的主题,然后按给定的书籍列表逐个执行特定操作。我做了以下几点:我无法使用Selenium WebDriver逐一访问WebElements列表
WebDriver driver= new ChromeDriver();
driver.manage().window().maximize();
driver.get("https://www.amazon.com");
driver.findElement(By.id("twotabsearchtextbox")).sendKeys("Operating System Books");
driver.findElement(By.className("nav-input")).click();
links = driver.findElements(By.className("s-access-detail-page")); //try to cache this
for(WebElement link : links) {
//System.out.println(String.format(link.getAttribute("href"))); //prints
link.click();
//extract reviews and one by one forward to semantic analysis
driver.navigate().back();
}
}
但是,这个去第一个链接,然后回到列表的页面。 异常在线程“主” org.openqa.selenium.StaleElementReferenceException:陈旧元件参考:该程序然后用下面的错误终止元件不连接到页文档
这是完全正常的,你会有这个例外:'StaleElement'意味着你试图引用的元素不再处于活动浏览器窗口中。考虑到您已导航到某个其他页面,然后导航回来,前一页中的任何元素都不应再被激活。您必须重新进行搜索或在新窗口中打开链接,以便您的原始窗口在其他浏览器选项卡中保持不变。 –