code: public static ExpectedCondition<Boolean> stalenessOf(final WebElement element) {
      return new ExpectedCondition<Boolean>() {
        @Override
        public Boolean apply(WebDriver ignored) {
          try {
            // Calling any method forces a staleness check
            element.isEnabled();
            return false;
          } catch (StaleElementReferenceException expected) {
            return true;
          }
        }
  
        @Override
        public String toString() {
          return String.format("element (%s) to become stale", element);
        }
      };
    }
docs: Wait until an element is no longer attached to the DOM.
  
  @param element The element to wait for.
  @return false if the element is still attached to the DOM, true otherwise.