Stale element references are common when AJAX tables reload. Learn how to handle them in Katalon Studio with SmartWait, exception handling, JavaScript load checks, and retry logic, with code examples.
The information in this blog post is based on a real-life scenario shared by a user on our Katalon Community forum and is intended to inspire peer-to-peer discussion and collaboration. Please always test solutions thoroughly before implementing them in a production environment.
Join in the discussion here.
Introduction
Navigating dynamic web applications often introduces challenges like handling stale element references, especially when dealing with AJAX table loading. This common issue can disrupt your automated testing flow, but Katalon Studio offers reliable solutions to streamline the process. In this post, we’ll explore effective strategies to manage stale element references and ensure your test automation remains reliable.
Understanding Stale Element References
Stale element references occur when an element previously located on the DOM is either removed or re-rendered before an action can be performed on it. This is particularly common with AJAX-based tables where the content is dynamically loaded or updated.
Using Katalon Studio’s SmartWait
One of the standout features of Katalon Studio is SmartWait, which intelligently waits for the web page to be in a ready state before executing actions. This feature significantly reduces the likelihood of encountering stale element references.
How to Enable SmartWait?
- Navigate to Project > Settings > Execution > Default > Web UI.
- Check the box for Enable Smart Wait.
- Configure the timeout settings as needed.
Exception Handling Techniques
Proper exception handling is important for dealing with unpredictable changes in the DOM. By catching StaleElementReferenceException, you can implement a retry mechanism or alternative actions to recover from the error.
Sample Code
try {
WebElement element = driver.findElement(By.id("example"));
element.click();
} catch (StaleElementReferenceException e) {
// Re-locate the element, then retry or take alternative action
}Using JavaScript for More Reliable Interaction
JavaScript can be a useful tool for interacting with dynamic elements. By using JavaScript to check the status of AJAX calls, you can make sure the table has finished loading before interacting with it.
Example Using jQuery
// In Katalon Studio: WebDriver driver = DriverFactory.getWebDriver()
JavascriptExecutor js = (JavascriptExecutor) driver;
// Only works on pages that load jQuery
Boolean ajaxComplete = (Boolean) js.executeScript("return window.jQuery != null && jQuery.active == 0");
if (ajaxComplete) {
// Interact with the table
}Implementing Retry Logic
In cases where the DOM is highly dynamic, implementing retry logic can help stabilize your tests. This approach re-locates the element and retries the action a limited number of times if it goes stale between lookup and interaction. (In Selenium 4, WebDriverWait takes a Duration rather than a number of seconds.)
Example
int attempts = 0;
while (attempts < 3) {
try {
WebElement element = new WebDriverWait(driver, Duration.ofSeconds(10))
.until(ExpectedConditions.elementToBeClickable(By.id("example")));
element.click();
break; // success
} catch (StaleElementReferenceException e) {
attempts++; // the table re-rendered; locate the element again
}
}Conclusion
Handling stale element references in AJAX table loading can be challenging, but with Katalon Studio’s features and best practices, you can maintain stable, reliable test automation. Enable SmartWait, use exception handling, use JavaScript checks, and implement retry logic to tackle this issue effectively.
If you’re facing a similar challenge, why not give these solutions a try? And remember, our community forum is a great place to share your experiences and learn from others. If you have any questions or need further assistance, don’t hesitate to Ask the Community!
Frequently asked questions
It happens when an element previously located in the DOM is removed or re-rendered before an action is performed—common with AJAX tables that load/update content dynamically.
SmartWait intelligently waits for the page to be in a ready state before executing actions, reducing the likelihood of stale element references.
Go to Project > Settings > Execution > Default > Web UI, check Enable Smart Wait, and adjust timeout settings as needed.
Catch StaleElementReferenceException and implement retry logic or alternative actions to recover.
The example checks AJAX completion with jQuery using return jQuery.active == 0 before interacting with the table.

Test everything. Ship with confidence.
From generating test cases to spotting failures, Katalon's AI works across your entire pipeline, automatically.





