开发者

Selenium2 Webdriver C# .Click() List - Stale Reference Exception

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 w开发者_Go百科ith 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.


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.


    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();
    }
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜