How to solve StaleElementReferenceException error

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*/ }
    }
c#
selenium
selenium-webdriver
asked on Stack Overflow Dec 29, 2017 by Mutu Arron • edited Dec 29, 2017 by Mutu Arron

1 Answer

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;

}

answered on Stack Overflow Dec 29, 2017 by Binny Chanchal

User contributions licensed under CC BY-SA 3.0