Dax
Dax

Reputation: 59

Comparing of list's elements downloaded by WebDriver

I need to compare elements from tables which are presented on a few subpages (only values from first column).

Using lists I created one big list (which has ex. 95 elements). Unfortunately when I start comparing them, my test end with an error message "org.openqa.selenium.StaleElementReferenceException: stale element reference: element is not attached to the page document"

Everything works correctly when I have less than 10 elements (only one table on one page).

Do someone know what should I change in my code?

List<WebElement> pagesList = webDriver.findElements(By.cssSelector("#div_table > ul >  li"));
if (pagesList.size() > 0) {
    boolean nextPagePresent = true;
    List<String> documentsList = webDriver.findElements(By.cssSelector("#table > tbody > tr > td:nth-child(1)"));
    while (nextPagePresent == true) {
        try {
            nextPagePresent = webDriver.findElements(By.cssSelector("#div_table > ul > li.nextLink")).size() > 0;
            documentsList += webDriver.findElements(By.cssSelector("#table > tbody > tr > td:nth-child(1)"));
            if(nextPagePresent == true) {
                webDriver.findElement(By.cssSelector("#div_table > ul >  li.nextLink")).click();
            }
        } catch (org.openqa.selenium.NoSuchElementException ex) {
            throw new Exception('Page not found');  
        }
    }
    documentsList.removeRange(0, 10);
    for (int i = 0; i < documentsList.size(); i++) {
        for (int k = i + 1; k < documentsList.size(); k++) {
            if (documentsList.get(i).getText().equals(documentsList.get(k).getText())) {
                log.info(documentsList.get(i).getText())
                assert false;
            }
        }
    }
} else {
    List<String> documentsList = webDriver.findElements(By.cssSelector("#table > tbody > tr > td:nth-child(1)"));
    for (int i = 0; i < documentsList.size(); i++) {
        for (int k = i + 1; k < documentsList.size(); k++) {
            if (documentsList.get(i).getText().equals(documentsList.get(k).getText())) {
                assert false;
            }
        }
    }
}

Upvotes: 0

Views: 109

Answers (1)

Mikhail
Mikhail

Reputation: 665

The exception you're seeing is expected since you have elements located on multiple pages and you're actually switching between them.

Basically the exception you're getting indicates that, i.e. it says it is unable to retrieve text from WebElement, which is logically correct since the element is no longer attached to the DOM model. What you could do is to extract text from elements that are currently visible on the page and put it into separate List<String>, once you'll extract text from all the elements from all the pages you could compare it. So order of actions should look like this:

  1. Load page
  2. Extract text from all visible elements and put it into separate list
  3. Change page
  4. Repeat steps 2,3 until there are no pages left
  5. Compare results

More detailed info on exception could be found here: https://www.seleniumhq.org/exceptions/stale_element_reference.jsp

P.S. Usage of build-in asserts should be avoided, consider using test java test libraries like jUnit or TestNg or assert specific libraries like assert4j it would give you more flexibility.

Upvotes: 1

Related Questions