我想搜索亞馬遜上的主題,然後按給定的書籍列表逐個執行特定操作。我做了以下幾點:我無法使用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'意味着你試圖引用的元素不再處於活動瀏覽器窗口中。考慮到您已導航到某個其他頁面,然後導航回來,前一頁中的任何元素都不應再被激活。您必須重新進行搜索或在新窗口中打開鏈接,以便您的原始窗口在其他瀏覽器選項卡中保持不變。 –