Handle Stale Elements

A stale element reference error occurs when a referenced web element is no longer attached to the DOM. For a WebDriver-based UI test, an error message can look like: The element reference is stale; either the element is no longer attached to the DOM, it is not in the current frame context, or the document has been refreshed.

This error message points to the reason why an error occurred. In a test, we found and saved an instance of an element, but then the content was refreshed (partially or completely) and the element became "stale". Basically, the element is now an orphan because it's no longer part of the DOM tree.

To avoid this type of test, a test should create a new element reference by finding the element again after it's attached to the DOM.

Let's look at a few use cases.

If a page is completely refreshed, we must:

  • Wait for the old content to become stale (this step is important!)
  • Find all elements starting from the root (inside the Driver). In UTAM, you can use UtamLoader.load, which creates a new instance of the root page object.

Example of a Java test:

Example of a JavaScript test:

With AJAX-based applications, often only part of the content is refreshed. In this scenario, we don't need to recreate an instance of the root page object, but we need to call an element method for the element that's refreshed.

Example of a Java test:

Example of a JavaScript test: