我正在做一個硒專案,洗掉網頁中的所有鏈接并單擊它,然后獲取新聞的標題和描述。我想對主頁中??的所有鏈接執行此操作 - 比如說 bbc.com 但是一旦我單擊鏈接并切換回來,主頁就會重繪 。其余鏈接顯示為陳舊元素問題。她是我的代碼 任何幫助將不勝感激。
List<WebElement> allLinks = driver.findElements(By.tagName("a"));
for(int el = 0; el < allLinks.size(); el ) {
hrefs = allLinks.get(el).getAttribute("href");
try {
allLinks.get(el).click();
//newpage = driver.getWindowHandle();
try {
text = driver.findElement(By.xpath("*//div[contains(@class,'title-text')]")).getText();
description = driver.findElement(By.xpath("*//div[contains(@class,'title-text')]/ancestor::div[contains(@class,'title')]/following::h2[contains(@class,'subtitle')]")).getText();
noSwitch = false;
} catch (Exception e) {
// TODO Auto-generated catch block
System.out.println("Page not navigated");
}
} catch(StaleElementReferenceException e) {
System.out.println("Stale element issue");
text = allLinks.get(el).getText();
description = "No Description.";
}
System.out.println(hrefs " - " text " - " description);
utility.setCellData("first sheet", rowcnt, 1, text);
utility.setCellData("first sheet", rowcnt, 2, hrefs);
utility.setCellData("first sheet", rowcnt, 3, description);
rowcnt ;
if(!noSwitch) {
driver.switchTo().window(homepage);
}
}
uj5u.com熱心網友回復:
你必須回憶你的鏈接,所以像這樣重新加載所有鏈接:
List<WebElement> allLinks = driver.findElements(By.tagName("a"));
for(int el = 0; el < allLinks.size(); el ) {
#add this line
allLinks = driver.findElements(By.tagName("a"));
轉載請註明出處,本文鏈接:https://www.uj5u.com/qukuanlian/397007.html
標籤:硒 硒网络驱动程序 硒-firefoxdriver 陈旧元素引用异常
