2

I keep getting a StaleElementReferenceException error when I try to find all links and navigate through them in my console application, I have the following code and tried all day to fix it yesterday but with no result:

{

    static void Main(string[] args)
    {
        try
        {
            Console.WriteLine("Starting the browser...");
            IWebDriver driver = new FirefoxDriver();
            driver.Navigate().GoToUrl("http://www.site.ro");
            System.Threading.Thread.Sleep(2000);
            Console.WriteLine("Gathering the Links...");
            List<IWebElement> links = new List<IWebElement>();
            try
            {
                foreach (IWebElement item in driver.FindElements(By.TagName("a")))
                {
                    try
                    {
                        if (item.Displayed == true)
                        {
                            item.Click();
                            Console.WriteLine("Item is displayed \a\n" + "Navigating to link...");
                        } else
                        {
                            continue;
                        }
                        Random r1 = new Random();
                        Random r2 = new Random();
                        Random r3 = new Random();
                        var last = r3.Next(1, 10) * 700;
                        var mseconds = r2.Next(1, 10) * 500;
                        var time = mseconds + r1.Next(1, 10) * 300;
                        Console.WriteLine("Waiting for " + (time + last) + " miliseconds before next link");
                        System.Threading.Thread.Sleep(time + last);
                        driver.Navigate().Back();
                        System.Threading.Thread.Sleep(2000);
                    }
                    catch (Exception e2)
                    {
                        Console.WriteLine(e2);
                        Console.ReadLine();
                    }
                    }
                }
            catch (Exception e1)
            {
                Console.WriteLine(e1);
                Console.ReadLine();
            }
            Console.WriteLine("Test finished.");
            driver.Quit();
        }
        catch (Exception e)
        {
            Console.WriteLine(e);
            Console.ReadLine();
        }
    }
}

}

1 Answer 1

2

driver.FindElements(By.TagName("a")) is finding for you all the links on the page.

Then you are going to the another page using the first link : item.Click();

Finally you are going back driver.Navigate().Back();

But that's not the initial page (by selenium's opinion). And all the links stored at the first step are gone because your initial page is gone. That's why you can't click the second of them.

You need to refind all the links after each driver.Navigate().Back();

Or better store all the hrefs to a list like linksList.Add(Item.getAttribute("href")); and use stored hrefs.

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

1 Comment

Thanks, that fixed it for me:)

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.