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*/ }
    }
7
  • 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, 2017 at 9:40
  • Possible duplicate of StaleElementReference Exception in PageFactory Dec 29, 2017 at 9:47
  • @DebanjanB That post didn't solved my problem
    – Mutu Arron
    Dec 29, 2017 at 10:09
  • Which one is line 8? Dec 29, 2017 at 10:17
  • @DebanjanB var RegisterNo = CaseEntryTds.ElementAt(3).Text;
    – Mutu Arron
    Dec 29, 2017 at 10:46

1 Answer 1

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;

}

1
  • Can you tell me which line of my code has error and what is the error message? Jan 2, 2018 at 13:56

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.