C#Selenium:重定向并获取过时的元素引用:元素未附加到页面文档



我正在努力克服我遇到的这种特定情况。

错误:

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

导致以下错误的代码是:-

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);

简单!当你导航到一个新页面时,你会得到一个全新的DOM,所以你不能点击存储在你称为links的集合中的链接。

最新更新