'OpenQA.Selenium.StaleElementReferenceException : Cached elements 'By.clazz: android.widget.TextView' do not exist in DOM anymore

I am working on automation tests for android mobile app where I am trying to select a specific text option that is contained on a list of different options that can be selected manually, and have received the following error:

OpenQA.Selenium.StaleElementReferenceException : Cached elements 'By.clazz: android.widget.TextView' do not exist in DOM anymore

It is referring to the following highlighted yellow line in my code below:

my code

#region Get by element type public AndroidElement GetElement(string className, string elementText = null)

    {
        AndroidElement element = null;

        if (string.IsNullOrEmpty(elementText))
        {
            element = _driver.FindElementsByClassName(className).FirstOrDefault(x => x.Enabled);
        }
        else
        {
            element = _driver.FindElementsByClassName(className).FirstOrDefault(x => x.Enabled && x.Text == elementText);
        }
        return element;

    }
    public AndroidElement GetElementByXPath(string xpathName)
    {
        return _driver.FindElementByXPath(xpathName);
    }
    
    public AndroidElement GetByElementId(string elementId)
    {
        return _driver.FindElementById(elementId);
    }
    #endregion

Any ideas what is wrong?



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source