Katalon Studio Fails to Handle StaleElementReferenceException

Katalon Studio is unable to handle StaleElementReferenceException. Despite implementing try-catch blocks using both the StaleElementReferenceException class and the generic Exception class, the exception is not caught, and the catch block is never executed.

This issue is critical for handling dynamic elements in real-world testing scenarios, and it disrupts the reliability of automated tests.

Steps to Reproduce:

  1. Use the following code block in a test case
    try {
    String text = SmartUIValidator.getText(findTestObject(“Dev/HomePage/NonNavigationBar/AccountBalance/accountBalanceIntegerAmount”), 30, FailureHandling.CONTINUE_ON_FAILURE, “”);
    System.out.println(“accountBalanceIntegerAmount :” + text);
    } catch (StaleElementReferenceException e) {
    System.out.println(“StaleElementReferenceException in my test case.”);
    }

Replace the catch block with:

catch (Exception e) {
System.out.println(“Stale Element Exception.”);
}

Expected Results:
The catch block should execute and log the appropriate message when the exception occurs.

Actual Results:
The catch block does not execute, and the following error is returned:

Caused by: org.openqa.selenium.StaleElementReferenceException: Cached elements ‘By.xpath: //*[@content-desc = ‘testID-master-amount-main’ or @accesibilityID = ‘testID-master-amount-main’ or @label = ‘testID-master-amount-main’ or @name = ‘testID-master-amount-main’]’ do not exist in DOM anymore
For documentation on this error, please visit: https://www.selenium.dev/documentation/webdriver/troubleshooting/errors/
Build info: version: ‘3.141.59’, revision: ‘unknown’, time: ‘unknown’
System info: host: ‘DESKTOP-XOAXXX’, ip: ‘xxx.168.125.152’, os.name: ‘Windows 10’, os.arch: ‘amd64’, os.version: ‘10.0’, java.version: ‘17.0.7’
Driver info: com.kms.katalon.core.appium.driver.SwipeableAndroidDriver

Number of Affected Users:
Potentially all users testing applications with dynamic web/mobile elements.

Environment (for Mobile Testing):

  • Appium Version: 2.12.1
  • Mobile Platform/Version: Android 13
  • Real Device or Emulator: Real device (Samsung Galaxy Note 20)

Request:
This issue was raised as early as October 2023 for web, and no resolution or workaround has been provided since then. Long delays like this are frustrating, especially for recurring issues.

Community Note:
I encourage other users to vote on this topic to help prioritize this issue.

Looking forward to a prompt resolution.

Best Regards,

What is this class “SmartUIValidator”?

This seems not to be a part of Katalon API. I can not find it in the https://api-docs.katalon.com/

This is just wrapper function that uses katalon Mobile.getText function.
Anyways, I have tried to use katalon keyword

try {
String text = Mobile.getText(findTestObject(“Dev/HomePage/NonNavigationBar/AccountBalance/accountBalanceIntegerAmount”), 30, FailureHandling.CONTINUE_ON_FAILURE)

System.out.println(“accountBalanceIntegerAmount :” + text)
}
catch(StaleElementReferenceException e) {
System.out.println("StaleElementReferenceException in my test case ");

System.out.println("StaleElementReferenceException in my test case " + e.getMessage());

}
and not firing StaleElementReferenceException exception !

Looks to be the same issue:

This is likely because you’re not catching the top-level exception that is occurring. The error you shared is the “Caused by” error, which is the “root” cause, but it’s probably not the top-most exception in the stack.

I have tried Exception as will which regarded as top level exception and not working as well

Dear @Elly_Tran is there any update ? is there any update about this defect?

I made a Test Case (runnable anywhere) in order to reproduce the issue @osamazaza200 got.

import com.kms.katalon.core.exception.StepFailedException
import com.kms.katalon.core.model.FailureHandling
import com.kms.katalon.core.testobject.ConditionType
import com.kms.katalon.core.testobject.TestObject
import com.kms.katalon.core.webui.keyword.WebUiBuiltInKeywords as WebUI

TestObject makeTestObject(String id, String xpath) {
	TestObject tObj = new TestObject(id)
	tObj.addProperty("xpath", ConditionType.EQUALS, xpath)
	return tObj
}

WebUI.openBrowser("http://demoaut-mimic.kazurayam.com/")
TestObject validButton = makeTestObject("Make Appointment button", 
	"//a[@id='btn-make-appointment']")

println "the case of CONTINUE_ON_FAILURE started"
try {
	WebUI.verifyElementNotPresent(validButton, 3, FailureHandling.CONTINUE_ON_FAILURE)
} catch (Exception e) {
	println(">>> the case of CONTINUE_ON_FAILURE caught an Exception: " 
		+ e.getCause().getClass().getName() + ": "+ e.getMessage())	
}
println "the case of CONTINUE_ON_FAILURE finished"

println "the case of STOP_ON_FAILURE started"
try {
	WebUI.verifyElementNotPresent(validButton, 3, FailureHandling.STOP_ON_FAILURE)
} catch (Exception e) {
	println(">>> the case of STOP_ON_FAILURE, caught an Exception: " 
		+ e.getCause().getClass().getName() + ": " + e.getMessage())
}
println "the case of STOP_ON_FAILURE finished"

When I ran this Test Case, I got the following output in the console:

2024-12-03 20:08:09.451 INFO  c.k.katalon.core.main.TestCaseExecutor   - --------------------
2024-12-03 20:08:09.454 INFO  c.k.katalon.core.main.TestCaseExecutor   - START Test Cases/tryToCatchExceptionThrownByKeyword
2024-12-03 20:08:10.487 INFO  c.k.k.core.webui.driver.DriverFactory    - Starting 'Chrome' driver
12月 03, 2024 8:08:10 午後 org.openqa.selenium.remote.DesiredCapabilities chrome
情報: Using `new ChromeOptions()` is preferred to `DesiredCapabilities.chrome()`
2024-12-03 20:08:10.551 INFO  c.k.k.core.webui.driver.DriverFactory    - Action delay is set to 0 milliseconds
Starting ChromeDriver 131.0.6778.85 (3d81e41b6f3ac8bcae63b32e8145c9eb0cd60a2d-refs/branch-heads/6778@{#2285}) on port 33020
Only local connections are allowed.
Please see https://chromedriver.chromium.org/security-considerations for suggestions on keeping ChromeDriver safe.
ChromeDriver was started successfully on port 33020.
12月 03, 2024 8:08:16 午後 org.openqa.selenium.remote.ProtocolHandshake createSession
情報: Detected dialect: W3C
2024-12-03 20:08:16.611 INFO  c.k.k.core.webui.driver.DriverFactory    - sessionId = 3ed898bbf1b0ca11ad513fb3fd89da0a
2024-12-03 20:08:16.671 INFO  c.k.k.core.webui.driver.DriverFactory    - browser = Chrome 131.0.0.0
2024-12-03 20:08:16.692 INFO  c.k.k.core.webui.driver.DriverFactory    - platform = Mac OS X
2024-12-03 20:08:16.713 INFO  c.k.k.core.webui.driver.DriverFactory    - seleniumVersion = 3.141.59
2024-12-03 20:08:16.735 INFO  c.k.k.core.webui.driver.DriverFactory    - proxyInformation = ProxyInformation { proxyOption=NO_PROXY, proxyServerType=HTTP, username=, password=********, proxyServerAddress=, proxyServerPort=0, executionList="", isApplyToDesiredCapabilities=true }
the case of CONTINUE_ON_FAILURE started
...
the case of CONTINUE_ON_FAILURE finished
the case of STOP_ON_FAILURE started
...
>>> the case of STOP_ON_FAILURE, caught an Exception: com.kms.katalon.core.exception.StepFailedException: Unable to verify object 'Make Appointment button' is not present
...
the case of STOP_ON_FAILURE finished
2024-12-03 20:08:34.722 WARN  c.k.katalon.core.main.TestCaseExecutor   - Failed connecting to tab web socket.
2024-12-03 20:08:34.729 INFO  c.k.katalon.core.main.TestCaseExecutor   - END Test Cases/tryToCatchExceptionThrownByKeyword

Please note that a message ">>> the case of CONTINUE_ON_FAILURE caught an Exception: " is NOT there. This implies that the statement WebUI.verifyElementNotPresent(validButton, 3, FailureHandling.CONTINUE_ON_FAILURE) threw no Exception at all.

We found that a call to built-in keywords with FailureHandling.CONTINUE_ON_FAILURE suppresses any Exception even if an Exception was raised internally.

What’s going on?

You can read the source codes of Katalon Studio (though partially). You can find it in the folder

  • <Katalon installation directory>/Contents/Eclipse/configuration/resources/source

You can read the source code of the com.kms.katalon.core.keyword.internal.KeywordMain class. This class internally controls executing all built-in keywords`:

package com.kms.katalon.core.keyword.internal
...
public class KeywordMain {
    ....
    public static stepFailed(String message, FailureHandling flHandling, Throwable t, Map<String, String> attributes = null) throws StepFailedException {
        String failedMessage = buildReasonMessage(message, t != null ? ExceptionsUtil.getStackTraceForThrowable(t) : EMPTY_REASON).toString()
        switch (flHandling) {
            case FailureHandling.OPTIONAL:
                logger.logWarning(failedMessage, attributes, t);
                break;
            case FailureHandling.CONTINUE_ON_FAILURE:
                logger.logFailed(failedMessage, attributes, t);
                Exception ex = null;
                if (ErrorCollector.isErrorFailed(t)) {
                    ex = new StepErrorException(failedMessage, t)
                }
                ex = new StepFailedException(failedMessage, t)
                ErrorCollector.getCollector().addError(ex);
                break;
            case FailureHandling.STOP_ON_FAILURE:
                logger.logFailed(failedMessage, attributes, t);
                ErrorCollector.getCollector().setStopLauncher(true);
                if (t instanceof StepFailedException || t instanceof StepErrorException) {
                    throw t;
                }
                if (ErrorCollector.isErrorFailed(t)) {
                    throw new StepErrorException(failedMessage, t)
                }
                throw new StepFailedException(failedMessage, t);
        }
    }
    ...

Please check the stepFailed() method carefully.

As the above examination proved, given with a FailureHandling.CONTINUE_ON_FAILURE the stepFailed() method suppresses any Exceptions thrown by keyword. On the other hand, given with a FailureHandling.STOP_ON_FAILURE the stepFailed() method throws a new StepFailedException.

So, if @osamazaza200 wants to catch Exceptions thrown by the keyword and handle them at the Test Case level, he should change his code as follows:

try {
    String text = Mobile.getText(findTestObject(“Dev/HomePage/NonNavigationBar/AccountBalance/accountBalanceIntegerAmount”), 
                                 30, 
                                 FailureHandling.STOP_ON_FAILURE)
} catch(Exception e) {
    ...
}

I think that the title of this topic “Katalon Studio Fails to Handle StaleElementReferenceException” is misleading. In fact, Katalon Studio worked just as designed. There is no bug here.

Dear @kazurayam ,
you are not testing Stale element exception,
when Katalon faces this issue, exception and StaleElementReferenceException not handling it.

Dear @Elly_Tran
Your support please here, note that the issue has been raised by someone else more than year and not resolved.

Please have a look at:

I showed a runnable sample codes that test Stale Element Reference Exception.

Dear @kazurayam
I have read your article, in the resolve your provided (you mentioned to the idea that automation tester need to study the changes being applied to pages) and as a result, taking steps to avoid them to occur.

But it’s not good a tool like katalon to not detect that exception inside try/ catch

@osamazaza200

I wrote:

Could you try this?