Im writing tests for mobile app in python + Appium. It is a one code for both platform - iOS and android. Tests works but I get but StaleElementReferenceException in console.
My page object class takes selectors :
- NEXT_BUTTON = { "android": (AppiumBy.XPATH, "//*[@resource-id = 'buttonNext']"), "ios": (AppiumBy.ACCESSIBILITY_ID, "buttonNext") }
and then I convert to property as web element,
@property def next_button(self): return self.find_element(self.NEXT_BUTTON)
because I use them in tests cases for ex
assert activation_flow.emailStep.confirm_button.is_displayed(), "Element is not displayed"
My code to find element
`def retry_on_stale(self, action: Callable[, any]):last_exception = Nonefor _ in range(self.retries):try:return action()except StaleElementReferenceException as e:last_exception = econtinueraise last_exception
def find_element(self, locator: dict) → WebElement:return self.retry_on_stale(lambda: WebDriverWait(self.driver, self.wait_time).until(EC.presence_of_element_located(locator[self.platform]),message=f"Not found element"))`
It’s a good strategy? How to fix problem with StaleElementReferenceException.