简体   繁体   中英

C# Selenium: Redirect and get stale element reference: element is not attached to the page document

I'm trying to overcome this certain situation I've encounterred.

Error:

OpenQA.Selenium.StaleElementReferenceException: stale element reference: element is not attached to the page document
  (Session info: chrome=87.0.4280.88)

The piece of the code that resulted in the following error is:-

 webDriver.Url = "https://www.xzy.com/groups/profile/list";

        Console.WriteLine("Thread Checkpoint 1");
        Thread.Sleep(5000);
        ICollection<IWebElement> links = webDriver.FindElements(By.CssSelector("a[href*='https://www.xzy.com/grouplink/url/groups/']"));

        foreach (IWebElement link in links)
        {       
                Console.WriteLine(link.Text.ToString());
                link.Click();
                Thread.Sleep(5000);
                webDriver.Url = "https://www.xzy.com/groups/profile/list";
                Console.WriteLine("Successfully navigated");
                webDriver.Navigate().Refresh();
                Thread.Sleep(5000);

        }
        Console.WriteLine("Done");
        //Thread.sleep(5000);

Simple, When you navigate to a new page you get a whole new DOM, So you cannot click the links that were stored in the collection that you called links .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM