Using the following code I keep getting a StaleElementReference Exception when running my Webdriver scripts on firefox v57. I have tried all sorts of things but other than a thread.sleep or catching the exception and retrying I cannot get it to work.
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;
}
Essentially the page has some filter criteria and a results table with results in blocks of 10 rows, to get each new set you have to click >. i.e. the page changes.
I get StaleElement exception in two places, the first happens getting the text at the locator (then setting colNumber = Col). The 2nd happens adding celltext to the colValues array.
In both places I have only just got the element again. Any help would be appreciated. Thanks in advance.
Note I don't get this with the Chrome browser.