您好我是新來Selenium
硒StaleElementReferenceException
我現在用的是Java library
,已經試用過Chrome
和Firefox
驅動程序。
我正在運行一個循環。有趣的是,循環有時會工作3次,2次,並不總是在同一次迭代中失敗。我認爲它必須處理某種競爭條件(如等待頁面加載)。如果我運行在調試模式下,它似乎完美工作。
我已經嘗試了其他答案,如wait explicitly
和implicitly
,但仍然沒有幫助。也許如果你看到代碼,你可以幫我一把。
這進入循環。
WebDriverWait wait = new WebDriverWait(driver,20);
WebElement searchResults = driver.findElement(new By.ById("searchresults"));
searchResults = searchResults.findElement(new By.ByClassName("table"));
wait.until(ExpectedConditions.presenceOfAllElementsLocatedBy(By.tagName("a")));
List<WebElement> list=searchResults.findElements(By.tagName("a"));
for(WebElement w: list) {
result.add(w.getAttribute("href")); //EXCEPTION HAPPENS ALWAYS HERE
}
SOLUTION
的解決方案是一個總的黑客。我仍然不明白,但是它完成了這項工作。如果有人明白爲什麼請讓我知道。
我只是移動所有的等待,它表現更好。我還採取了@Cyril的建議來重新運行迭代,如果在某些數據檢查中拋出異常,以確保我獲得了我想要的所有結果。
WebDriverWait wait = new WebDriverWait(driver,20);
wait.until(ExpectedConditions.presenceOfAllElementsLocatedBy(new By.ById("searchresults")));
wait.until(ExpectedConditions.presenceOfAllElementsLocatedBy(new By.ByClassName("table")));
wait.until(ExpectedConditions.presenceOfAllElementsLocatedBy(By.tagName("a")));
非常感謝您的回答我接受了您最後的建議。我以某種方式破解了一些其他的變化(見上文)。我不是100%高興,但這是工作。 – Altober