The element identified by id either not present or it has expired from the internal cache

Hello
I am facing the issue

Blockquote
org.openqa.selenium.StaleElementReferenceException: The element identified by “9BEA0100-0000-0000-5C01-000000000000” is either not present or it has expired from the internal cache. Try to find it again
For documentation on this error, please visit: https://www.seleniumhq.org/exceptions/stale_element_reference.html
Build info: version: ‘3.141.59’, revision: ‘e82be7d358’, time: ‘2018-11-14T08:17:03’
ession ID: c0c92aa0-d7f5-4c37-86ef-e17e301c5343
at java.base/jdk.internal.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
at java.base/jdk.internal.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62)
at java.base/jdk.internal.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
at java.base/java.lang.reflect.Constructor.newInstanceWithCaller(Constructor.java:500)
at java.base/java.lang.reflect.Constructor.newInstance(Constructor.java:481)
at org.openqa.selenium.remote.http.W3CHttpResponseCodec.createException(W3CHttpResponseCodec.java:187)
at org.openqa.selenium.remote.http.W3CHttpResponseCodec.decode(W3CHttpResponseCodec.java:122)
at org.openqa.selenium.remote.http.W3CHttpResponseCodec.decode(W3CHttpResponseCodec.java:49)
at org.openqa.selenium.remote.HttpCommandExecutor.execute(HttpCommandExecutor.java:158)
at io.appium.java_client.remote.AppiumCommandExecutor.execute(AppiumCommandExecutor.java:239)
at org.openqa.selenium.remote.RemoteWebDriver.execute(RemoteWebDriver.java:552)
at io.appium.java_client.DefaultGenericMobileDriver.execute(DefaultGenericMobileDriver.java:41)
at io.appium.java_client.AppiumDriver.execute(AppiumDriver.java:1)
at io.appium.java_client.ios.IOSDriver.execute(IOSDriver.java:1)
at org.openqa.selenium.remote.RemoteWebElement.execute(RemoteWebElement.java:285)
at io.appium.java_client.DefaultGenericMobileElement.execute(DefaultGenericMobileElement.java:45)
at io.appium.java_client.MobileElement.execute(MobileElement.java:1)
at io.appium.java_client.ios.IOSElement.execute(IOSElement.java:1)
at org.openqa.selenium.remote.RemoteWebElement.click(RemoteWebElement.java:84)

Appium version: 1.20.1
Ipad iOS: 14.1

The test case is running within 2-3 hours and then the exception is thrown
Have you ever to meet this issue ? Please help

You might have fetched the element before and then it was changed but you are still using the previous state of the element.

You can fix this adding a try-catch for StaleElementReferenceException then do a re-fetching until the element is visible.

async function waitForLocated (driver: WebDriverClass, locator: WebDriverLocator, retries?: number = 3)
   try {
     await driver.wait(until.elementLocated(locator), 7000)
   } catch (err) {
     if (retries === 0) {
      throw new Error(`Still not able to locate element ${locator.toString()} after maximum retries, Error message: ${err.message.toString()}`)
     }
    await driver.sleep(250)
     return waitForLocated(driver, locator, retries - 1)
   }
 }