2

I need some help because I keep getting a StaleElementReference when I try to parse a list of a tags to click.

What I have done is on page land I iterate through the page and generate an object List<> with with all the a tags

    private List<IWebElement> _pageLinks;
    public List<IWebElement> pageLinks
    {
        get
        {
            if (_pageLinks == null)
            {
                _pageLinks = InfoDriver.FindElements(By.TagName("a")).ToList();
            }
            return _pageLinks;
        }
    }

Then I want to parse this list, and click each one and then go back to the page it was referenced from.

    private static SeleniumInformation si = new SeleniumInformation(ffDriver);

        si.pageLinks.ForEach(i =>
        {
            i.Click();
            System.Threading.Thread.Sleep(1000);
            ffDriver.Navigate().Back();
        });

What happens is that after the first click it goes to the new page and then goes back to the starting page but it can't get the next link. I've tried setting it to a static element, setting a backing field so that it checks to see if there is data there already however it appears that on click the IwebElement looses the list and it doesn't rebuild the list either so I get a StaleElementReference exception not handled and element not found in cache.

Is this a bug in Selenium with the IWebElement class or am I doing something wrong? Any help would be greatly appreciated.

2 Answers 2

1

This is the expected behavior. You left the page the element was on. When you navigated back, it is a new page and that element is no longer on it.

To work around this I would suggest passing around Bys instead, if you can. Assuming your anchorlinks all have unique hrefs, you could instead generate a list as follows (java code, but should translate to c#):

private static List<By> getLinks(WebDriver driver)
{
    List<By> anchorLinkBys = new ArrayList<By>();
    List<WebElement> elements = driver.findElements(By.tagName("a"));
    for(WebElement e : elements)
    {
        anchorLinkBys.add(By.cssSelector("a[href=\"" + e.getAttribute("href") + "\"]"));
        //could also use another attribute such as id.
    }
    return anchorLinkBys;
}

I don't know the makeup of your page so I don't know if it is possible to generate By's dynamically that uniquely identify the elements you want. For example if all the elements have the same parent, you could use the css level 3 selector nth-child(n). Hopefully you get some ideas from the above code.

Sign up to request clarification or add additional context in comments.

1 Comment

unfortunately that is part of the problem. there can be duplicate hrefs, duplicate tag text and in some instances no IDs on the elements. The concept of what I am doing is to get every page of the entire site stored in a list, then store a list of links for each of those pages. Then test every link to verify proper functionality.
1
    private void YourTest()
    {
        IWebDriver browserDriver = new FirefoxDriver();
        browserDriver.Navigate().GoToUrl(pageUrl);
        int linkCount= browserDriver.FindElements(By.TagName("a")).Count;

        for (int i = 0; i <= linkCount-1; i++ )
        {
            List<IWebElement> linksToClick = browserDriver.FindElements(By.TagName("a")).ToList();
            linksToClick[i].Click();
            System.Threading.Thread.Sleep(4000);
            if(some boolean check)
            {
              //Do something here for validation
            }
            browserDriver.Navigate().Back();
        }
        broswerDriver.Quit();
    }

1 Comment

I had to use IList<IWebElement> instead of List<IWebElement>.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.