0

Hope someone can help me. I have the following code and I am getting the Exception thrown: 'OpenQA.Selenium.StaleElementReferenceException'

IList<IWebElement> WeekDays = Chromedriver.FindElements(By.XPath("//td[@class='dxeCalendarDay']"));

foreach (IWebElement Days in WeekDays)
{
    string WeekDaysResults = Days.Text;

    if(string.IsNullOrEmpty(WeekDaysResults))
    {
        //Do Nothing
    }
    else
    {
        if(WeekDaysResults == FirstDayOfCurrentMonth)
        {
            Days.Click();
            Debug.WriteLine("Week Days: " + WeekDaysResults);
        }        
    }
}

I'm getting the exception on this code string WeekDaysResults = Days.Text;

The error:

OpenQA.Selenium.StaleElementReferenceException: 'stale element reference: element is not attached to the page document

I tried a try catch block but that did not work. Thanks in advance.

0

2 Answers 2

1

You may try this by reassigning WeekDays value,

IList<IWebElement> WeekDays = Chromedriver.FindElements(By.XPath("//td[@class='dxeCalendarDay']"));

foreach (IWebElement Days in WeekDays)
{
    string WeekDaysResults = Days.Text;

    if(string.IsNullOrEmpty(WeekDaysResults))
    {
        //Do Nothing
    }
    else
    {
        if(WeekDaysResults == FirstDayOfCurrentMonth)
        {
            Days.Click();
            Debug.WriteLine("Week Days: " + WeekDaysResults);
        }        
    } 
    WeekDays = Chromedriver.FindElements(By.XPath("//td[@class='dxeCalendarDay']"));
}
Sign up to request clarification or add additional context in comments.

Comments

0

A stale element reference exception is thrown in one of two cases, the first being more common than the second:

The element has been deleted entirely. The element is no longer attached to the DOM.

References: https://www.seleniumhq.org/exceptions/stale_element_reference.jsp

Comments

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.