0

My current code always do the findElements (see code below), and I only pass a parameter for the index. The problem on this is that it takes really long time during execution, since it re-scans the page to find all links (again and again).

My goal is to store all the links, but I only invoke findElements once.

Also to add, for every link, I click it so I will be redirected to other page and I just return back to first page.

I'm having a problem on this code :

List<WebElement> elementList;

elementList = driver.findElements(By.tagName("a"));

elementList.get(0).click(); // redirects to other page

driver.get(defaultURL);

elementList.get(1).click(); // staleElementException

elementList.get(2).click(); // staleElementException
elementList.get(3).click(); // staleElementException

and so on..

Any input on this?

  • Please provide HTML of list of element. – Sagar007 Apr 4 '17 at 9:13
  • @Julius, You cannot use the same elements to perform click operation. When you navigate back, DOM is getting refreshed. Read here about StaleElementException and how to avaoid. qalititude.blogspot.in/2016/06/… – zishan paya Apr 4 '17 at 13:45
  • @JuliusG since you added an answer it looks like you found a solution to your problem. Please click the checkbox icon next to the most useful answer to "accept" it, so that it's clear to other people that this question has been resolved. – c32hedge Jun 5 '17 at 17:10
  • @c32hedge yes thanks for reminding. – Julius G Jun 7 '17 at 6:56
1

You need to retrieve element List every time after one action is performed, indexes you are using will be same.

elementList = driver.findElements(By.tagName("a"));
elementList.get(0).click(); // redirects to other page
driver.get(defaultURL);
elementList = driver.findElements(By.tagName("a"));
elementList.get(1).click(); // staleElementException
elementList = driver.findElements(By.tagName("a"));

as this looks weird you can loop this using counter for elementList.size()

then it will be something like this:-

inside loop:-

{
elementList.get(i).click(); // redirects to other page

driver.get(defaultURL);

elementList = driver.findElements(By.tagName("a"));
}
| improve this answer | |
1

You can do something like the following:

public void navigateToEachPage()
{
    //Finding all the elements
    List<WebElement> links = driver.findElements(By.tagName("a"));;
    System.out.println(links.size());

    //Creating String array
    String[] linksText = new String [links.size()];
    int i=0;

    //Storing List elements text into String array
    for(WebElement a: links)
    {
        linksText[i]=a.getText();
        i++;
    }

    //Navigating to Each Page and returning to back
    for(int j=0;j<linksText.length;j++)
    {
        By linkTextLocator = By.linkText(linksText[j]);
        System.out.println("Clicking on this link:: "+driver.findElement(linkTextLocator).getText();
        driver.findElement((linkTextLocator)).click();
        driver.navigate().back();
    }
}

Updating as per @c32hedge's comment:

StaleElementReferenceException is occured when we find an element, the DOM gets modified then we reference the WebElement.

A classic example if this might be:

List<WebElement> listOfAnchors = driver.findElements(By.tag("a"));
for(WebElement anchor : listOfAnchors) {
    anchor.click();
    System.out.println(driver.getTitle());
    driver.navigate.back();
}

This code will get all the anchor elements (say 5) into a list. The list now has 5 WebElement references. We get the first reference and click it. This take us to a new page. We perform some actions on new page and then we use back() to go back to the original page.

The DOM looks just like the previous DOM but it is a different DOM. So now all the references in the list become stale. On the second iteration, it gets the second reference and clicks it. This will throw a StaleElementReferenceException.

For future reference please read the following article.

| improve this answer | |
  • could you summarize the relevant content of the article you linked to in case the linked article goes away in the future? – c32hedge Jun 5 '17 at 17:11
1

For this specific problem, I did findElements.

Since my initial problem on this is to get all the elements and avoid staleElement exceptions, I just converted it to a String. It goes like this:

private List<WebElement> elementList;

private List<String> elementXpathList;

Then I created a loop, so that for every elementList, I immediately convert it to its XPath by using JavaScript.

Loop for elementList here {
  elementXpathList.add(elementList.get(index));
}

The Result will be added to elementXpathList.

| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.