I am doing the automated testing using visual studio. While getting the inner element value it's showing this error.
It's not showing this error all the time but sometimes it's successfully tested.
OpenQA.Selenium.StaleElementReferenceException occurred HResult=0x80131500 Message=stale element reference: element is not attached to the page document
I tried to solve this issue, but unfortunately I didn't get any positive result.
Below is my code:
public List<CaseListEntry> GetCaseListEntries()
{
var CaseGridTrs = CaseListGrid.FindElements(By.XPath(".//tr"));
var entryList = CaseGridTrs.Select(x =>
{
var CaseEntryTds = x.FindElements(By.XPath(".//td"));
var RegisterDate = CaseEntryTds.ElementAt(1).Text;
var RegisterNo = CaseEntryTds.ElementAt(3).Text;
return new CaseListEntry
{
ListNo = CaseEntryTds.ElementAt(0).FindElement(By.XPath(".//a")).Text,
RegDate = DateTime.ParseExact(RegisterDate, "dd.MM.yyyy",
CultureInfo.InvariantCulture),
DocumentType = CaseEntryTds.ElementAt(2).Text,
};
}).ToList();
return entryList;
}
I am getting this stale exception error in line 8.I also i tried to use webdriver.wait it's still showing the same error.
I also try tried to wait for page to load
var customWait = new WebDriverWait(new SystemClock(), driver, TimeSpan.FromSeconds(5), TimeSpan.FromMilliseconds(1));
//ignore the timeout exception
try
{
customWait.Until(CustomExpectedCond.ElementHasClass(LoadingIndicator, "t-icon t-refresh t-loading"));
customWait.Until(CustomExpectedCond.ElementHasClass(LoadingIndicator, "t-icon t-refresh"));
}
catch (WebDriverTimeoutException) { /*ignore and hope for the best*/ }
}