StaleElementReferenceException: androidx.test.uiautomator.StaleObjectException

Hi Guys,
I am getting stale element exception on android device.
There is one spinner need to click on it new window will open and sselect the value from it.
So my script is below:

MobileElement Spinnerrole = driver.findElement(By.id(“textViewUserName”));
Spinnerrole.click();
Spinnerrole.sendKeys(“Resident”);
Click is working fine but for 2nd line of script giving below error :

org.openqa.selenium.StaleElementReferenceException: androidx.test.uiautomator.StaleObjectException

I am using appium 1.51.1 version

Can any one please help me here

@Aleksei : Please help me here if you have encountered the same issue ?

After click wait few seconds and and look again into your screeen. Keyboard should be open to use sendKeys. Also double check that your element still visible.

PS normally on Android setValue or sendKey does not work. We need scroll spinner to needed value.

@Aleksei I want to select the value here in the image, So the scenario is when I will click on spinner, new window will open where I have to select a value.
There is no id so not able to select the value and xpath is not working.
Can you please give me the code to select the value ?


I used this xpath to select the Aged Care value
("//*[contains(text(),‘Aged Care’)]") but it is giving no such element exception.

use:

    String txt = "my text";
    MobileElement  el = driver.findElement(MobileBy.AndroidUIAutomator("new UiSelector().textContains(\"" + txt + "\"));

Not working.
I have got below error:
NoSuchElementException: An element could not be located on the page using the given search parameters.

I am able to make select the value using list.
Below is the script which worked for me:

wait.until(ExpectedConditions.visibilityOfElementLocated(By.id(“spinnerRole”)));
MobileElement spinnerrole = driver.findElement(By.id(“spinnerRole”));
spinnerrole.click();
Thread.sleep(100);
Listroles =driver.findElements(By.id(“textViewUserName”));
System.out.println("roles : " + roles);
roles.get(5).click();