【发布时间】:2018-01-08 13:06:28
【问题描述】:
使用以下代码在 firefox v57 上运行我的 Webdriver 脚本时,我不断收到 StaleElementReference 异常。我尝试了各种方法,但除了 thread.sleep 或捕获异常并重试之外,我无法让它工作。
public List<String> readCoIData (String column) throws StaleElementReferenceException
{
int colNumber 0;
WebTable searchResuIts = getTable();
FluentWait<WebDriver> wait = new FluentWait<WebDriver>(driver)
wait.pollingEvery(250, TimeLinit.MILLISECONDS);
wait.with Timeout (30, TimeLlnit.SECONDS);
wait.ignoring (NoSuchElementException.class) ;
CustomLogger.addInfo(Logger, "Ensure that that the first column row contains text: " column ) ;
int colcount = searchResults.getColumnCount();
CustomLogger. addlnfoLog4jOnly (logger , "colcount= " colcount )
for (int col =1; col <= colcount; col++)
{
CustomLogger.addInfoLogJOnly(Logger, "Get the cell data for col. Use a string as this does not go stale unlike a reference.'
String locator = String.format("pf_table_t2 > tbody:nthchild(1) > tr:nth-child(1) > td:nth-child(%d)", col);
wait.until(ExpectedConditions.presenceOfElementLocated(By. cssSelector (locator))) ;
if (driver.FindElement(By.cssSelector(locator)).getText().contains (column))
{
colNumber = Col;
}
}
CustomLogger.addInfoLogJOnly(Logger, "Assert that there is not 0 columns");
assertThat (column + " not found' , colNumber !=0, is (true));
List<String> colvalues = new ArrayList<String>();
CustomLogger.addInfoLogJOnly(Logger, "Get the object .pf_paging a");
List<WebElement) paging = driver.findElements(By.cssSelector(.pf_paging a));
if (paging.size() !=0)
{
CustomLogger.addlnfoLogJOnly ( logger , "If more than one result page, wait..." );
wait.until(ExpectedConditions.visibilityOf((WebElement driver.findElement(By.cssSelector(".pf_paging_next_active"))));
CustomLogger.addInfoLogJOnly(Logger, "Span with '>>' is found so loop through all pages and get the data");
while (driver.findElements(By.cssSelector(".pf_paging_next_active")). size() == 1)
{
for (int i=2; i<=searchResults.getRowCount(); i++)
{
String locator = String.format("pf_table_t2 > tbody:nthchild(1) > tr:nth-child(%d) > td:nth-child(%d)", I, colNumber); col);
String cell Text = driver.findElement(By.cssSelector(locator)).getText();
colvalues.add(cellText);
}
CustomLogger.addInfoLogJOnLy(Logger, "Click the Next Page Button ' to move onto next page and wait for the page to be visible.'
driver.findElements(By.cssSelector(.pf_paging_next_active a").click();
CustomLogger.addInfoLogJ0nly(Logger, "Get & Wait for table after clicking next");
// Get the element table again as we have clicked 'filter' and the DOM would have changed.
searchResults.getTable();}
public WebTable getTable()
{
CustomLogger.addInfoLog450nLy(Iogger, "Wait and get the element table again as we have previously clicked 'filter' and the DOM would have changed");
new WebDriverWait(driver, 30).until((ExpectedConditions<Boolean> driver -> ((JavascriptExecutor) driver).executeScript("return document.readyState").equals("complete"));
FluentWait(WebDriver» wait new
wait.pollingEvery(250, TimeLinit.MILLISECONDSS);
wait.with Timeout(30, TimeLlnit.SECONDS);
wait.ignoring (NoSuchEIementException.class) ;
CustomLogger.addInfo(Logger, "Wait until the results table is present..
wait.until(ExpectedConditions.presenceOfElementLocated(By.cssSelector("pf_table_t2"))));
CustomLogger.addInfo(Logger, "Get the results table");
SearchResuItsTable = driver.FindElement(By.cssSelector("pf_table_t2"));
WebTabIe searchResuIts = new WebTable(SearchResultsTabIe);
return searchResuIts;
}
基本上,该页面有一些过滤条件和一个结果表,其中包含 10 行块的结果,要获取每个新集合,您必须单击 >。即页面更改。
我在两个地方得到了 StaleElement 异常,第一个是在定位器处获取文本(然后设置 colNumber = Col)。第二次发生将 celltext 添加到 colValues 数组。
在这两个地方,我只是再次获得了元素。任何帮助,将不胜感激。提前致谢。
请注意,我无法通过 Chrome 浏览器获得此信息。
【问题讨论】:
-
您应该花一些时间了解什么是陈旧元素以及如何防止它们。休眠和等待不能修复陈旧的元素。问题是您正在访问一个包含页面更改后存储的 Web 元素的变量。避免该问题的最佳方法是了解站点以及情况何时发生变化。当它们确实发生变化时,请从页面中重新获取元素,而不是重新访问旧的变量引用。
-
我一直在阅读这篇文章,作为最后的手段在这里发帖。我只能在过滤结果后看到页面发生了变化,这就是为什么我会重新获取 tr 和 td 元素,如果您在上面的代码中看到 Locator 语句。然而,我仍然收到 StaleElement 异常。我似乎可以处理它的唯一方法是捕获异常,但我更愿意更主动。