1

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*/ }
    }
Mutu Arron
  • 71
  • 1
  • 9
  • From your given code at here, no any code will lead to whole/partial page refresh or reload. The only possible reason cause stale exeption, I guess should when the GetCaseListEntries() running, the table actually not complete load totally. So please try a long sleep before call GetCaseListEntries() to see still meet stale exception or not, If the sleep can fix the issue, adjust your webdriver.wait to give efficient time to wait the table load – yong Dec 29 '17 at 09:40
  • Possible duplicate of [StaleElementReference Exception in PageFactory](https://stackoverflow.com/questions/44838538/staleelementreference-exception-in-pagefactory) – undetected Selenium Dec 29 '17 at 09:47
  • @DebanjanB That post didn't solved my problem – Mutu Arron Dec 29 '17 at 10:09
  • Which one is `line 8`? – undetected Selenium Dec 29 '17 at 10:17
  • @DebanjanB `var RegisterNo = CaseEntryTds.ElementAt(3).Text;` – Mutu Arron Dec 29 '17 at 10:46
  • Can you show us what `ElementAt` is all about? – undetected Selenium Dec 29 '17 at 11:17
  • @DebanjanB It's a case table contains four row I'm getting the values from table using `ElementAt` and store it in string – Mutu Arron Dec 29 '17 at 11:38

1 Answers1

0

You can do it like this :

public List<CaseListEntry> GetCaseListEntries()
    {
        var CaseGridTrs = CaseListGrid.FindElements(By.XPath(".//tr"));
        var entryList = CaseGridTrs.Select(x =>
        {
            var CaseEntryTds        = x.FindElements(By.XPath(".//td"));

            var ListNoElement       = GetElementOrNull(CaseEntryTds,0);
            var RegisterDateElement = GetElementOrNull(CaseEntryTds,1);
            var DocumentTypeElement = GetElementOrNull(CaseEntryTds,2);
            var RegisterNoElement   = GetElementOrNull(CaseEntryTds,3);


            var RegisterDate        = (RegisterDateElement != null)? RegisterDateElement.Text : "";
            var RegisterNo          = (RegisterNoElement != null)? RegisterNoElement.Text : "";
            var ListNo              = (ListNoElement != null) ? ListNoElement.FindElement(By.XPath(".//a")).Text : "";
            var DocumentType        = (DocumentTypeElement != null) ? DocumentTypeElement.Text : "";
            return new CaseListEntry
            {
                ListNo = ListNo ,
                RegDate = DateTime.ParseExact(RegisterDate, "dd.MM.yyyy",
                CultureInfo.InvariantCulture),
                DocumentType = DocumentType
            };
        }).ToList();
        return entryList;
    }

public IWebElement GetElementOrNull(IList<IWebElement> CaseEntryTds, int elementAtPosition, int maxSeconds = 1) {
    IWebElement element = null;
    IWait<IWebDriver> wait = new WebDriverWait(_driver, TimeSpan.FromSeconds(maxSeconds));
    wait.Until(d => {
        try {
            element = CaseEntryTds.ElementAt(elementAtPosition);
            return element.Displayed;
        } catch (WebDriverTimeoutException) {
            return false;
        } catch (NoSuchElementException) {
            return false;
        }
    });
  return element;

}