[iOS] Cannot Prevent StaleElementReferenceException

As the topic suggests, I would like to ask what is the way to prevent this exception occur. I have searched in the Appium forum to understand the cause of this, from my understanding it seems it is about the "previously found element no longer exist when we are using it again)

And for the solutions, I found several ways and applied it, but the exception still occur quite often and randomly (just like every element was found could prompt this exception)

The ways I have tried:

  1. Added ‘waitForIdleTImeout’ = 0 into desired capability
  2. Added driver.update_settings({“boundElementsByIndex”: “True”})
    (The second method is suggested by the error message)
  3. Added driver.update_settings({“snapshotMaxDepth”: 500})

The above ways is the way to prevent it, in fact when I encountered this exception, in python, I just use a “try-catch” to ask it to search those “no longer exist” elements again, or add more time sleep between each finding elements command to try to make it be stable.

Would like to ask still any effective way to eliminate this error? ( In fact when I was developing the automation process for the same app 1 year ago, this “StaleElementReference” never happen,) not sure which part is changed that cause it become a nightmare for me. thanks…

Write your code. Without it hard to suggest.

Hi Aleksei, the following is the code in python:


FindXpaths = lambda x: driver.find_elements_by_xpath(x)

datatable = []

nonProcessData = FindXpaths(’//XUIElementTypeTable/XCUIElementTypeCell/XCUIElementTypeStaticText[@visible=“true”]’)

for value in nonProcessData:
datatable.append(value.text)


in the above code, it works most of the time, however, sometime may occur error when it is in this stage:


for value in nonProcessData:
datatable.append(value.text)


And the error message is :
Error Message: The previously found element “StaticText” is not present in the current view anymore. Make sure the application UI has the expected state. Original error: No matches found for Element at index 37 from input {( StaticText,
StaticText,
StaticText,
StaticText,
StaticText,
StaticText,
)}

for the loop of course syntax in my code is correct, just cannot “space” in forum here for "datatable.append(value.text), thanks