The element does not exist in DOM anymore

Hi,

Appium throws intermittent error

selenium.common.exceptions.StaleElementReferenceException: Message: The element 'By.id: com.androidsample.generalstore:id/productPrice' does not exist in DOM anymore

Code:
driver.find_element_by_id("com.androidsample.generalstore:id/productPrice").is_displayed()

Test data:
Appium Version: 1.17.1
Python Version: 3.8
IDE: Pycharm

Kindly guide me to resolve above issue.

Best Regards,
Gaurav Valera

Issue is fixed after applying Explicit Wait.In case some one is facing the same issue, do apply following steps:

            wait = WebDriverWait(self.driver, 8)
            wait.until(expected_conditions.presence_of_element_located((By.ID, "productPrice")))
            self.driver.find_element_by_id("productPrice").is_displayed()

Note: Import necessary packages too :slight_smile:

Which packages require to handle this exception?
@Gaurav_Valera

@Gaurav_Valera @Aleksei @KazuCocoa
Thanks for your code snippet. We had already implemented this wait feature of 20sec. But occasionally, or rather randomly this error still pops up. I could not figure out the reason. The code snippet for wait mechanism we use is as follows:

  • tapByElement(menuOptions.get(3), "Tap Devices Option");

  • public void tapByElement(WebElement e, String msg) {
              waitForVisibility(e);
              utils.log().info(msg);
              Rectangle elRect = e.getRect();
              Point point = new Point(
                      elRect.x + (int) (elRect.getWidth() / 2.0),
                      elRect.y + (int) (elRect.getHeight() / 2.0)
              );
              tapAtPoint(point);
          }
    
  • public boolean waitForVisibility(WebElement e) {
          WebDriverWait wait = new WebDriverWait(driver, Duration.ofSeconds(TestUtils.WAIT));
         return wait.until(ExpectedConditions.attributeContains(e, "enabled", String.valueOf(true)));
      }

it is cause you send element instead of it locator. plus your screen redraw during waiting.

Thanks Aleksei @Aleksei