Unable to catch StaleElementReferenceException using try/catch mehcanism

I am trying to run a script which usually thows StaleElementReferenceException during execution as you can see in attached image

To handle this exception, I added a try/catch block so that I can refresh/reload the page when this situation happen as you can see in attached image.

But interestingly, after exception being occured, execution never pass through catch block as you can confirm in first screenshot logs. After execption, it just quit catch block and move to next line which is cauing script to fail.

Any thoughts on that? What is not handled correctly here?

Regards,

1 Like

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. If you can share the entire error, I can show you what I mean. But before you do, read the next point:

This is not the best way to handle this situation. This is more of a bandage honestly. The best way to handle this is to avoid the StaleElementReferenceException in the first place. According to the screenshot of your script, you are:

1.) Clicking the element defined by ‘Keycloak_Objects/Users/AddUsers/tab_Groups’.
2.) Waiting for the element defined by ‘Keycloak_Objects/Users/AddUsers/Groups/btn_JoinGroups’ to be:
- a.) present
- b.) visible
- c.) visible (again, for some reason)
3.) Clicking the element defined by ‘Keycloak_Objects/Users/AddUsers/Groups/btn_JoinGroups’

At which point you are getting the exception. Coincidentally, I have some automation built for Keycloak myself, so I think what you’re trying to do is this:

From a user, click the “Groups” tab:

Click the “Join” button:

Is this accurate?

Awaiting the answer to the above, but one quick fix might be to use Smart Wait. Do you have this enabled in your project? I use a similar wait condition in my project and I’m able to switch tabs/do work without ever getting stale elements in Keycloak.

If you can share the entire error

The complete error is here but I dont see any other error/exception before stale element exception

TEST STEP: verifyElementVisible(findTestObject("Keycloak_Objects/Users/AddUsers/Groups/btn_JoinGroups"))
Start / End / Elapsed:	2023-10-23 15:21:40.597 / 2023-10-23 15:21:40.690 / 00:00:00.093
15:21:40.689	PASSED	Object 'Object Repository/Keycloak_Objects/Users/AddUsers/Groups/btn_JoinGroups' is visible         
                   	
TEST STEP: click(findTestObject("Keycloak_Objects/Users/AddUsers/Groups/btn_JoinGroups"))

15:21:40.879	FAILED	Unable to click on object 'Object Repository/Keycloak_Objects/Users/AddUsers/Groups/btn_JoinGroups' (Root cause: com.kms.katalon.core.exception.StepFailedException: Unable to click on object 'Object Repository/Keycloak_Objects/Users/AddUsers/Groups/btn_JoinGroups'
   at com.kms.katalon.core.webui.keyword.internal.WebUIKeywordMain.stepFailed(WebUIKeywordMain.groovy:64)
   at com.kms.katalon.core.webui.keyword.internal.WebUIKeywordMain.runKeyword(WebUIKeywordMain.groovy:26)
   at com.kms.katalon.core.webui.keyword.builtin.ClickKeyword.click(ClickKeyword.groovy:74)
   at com.kms.katalon.core.webui.keyword.builtin.ClickKeyword.execute(ClickKeyword.groovy:40)
   at com.kms.katalon.core.keyword.internal.KeywordExecutor.executeKeywordForPlatform(KeywordExecutor.groovy:74)
   at com.kms.katalon.core.webui.keyword.WebUiBuiltInKeywords.click(WebUiBuiltInKeywords.groovy:620)
   at com.kms.katalon.core.webui.keyword.WebUiBuiltInKeywords$click$16.call(Unknown Source)
   at AddGroupsForAgentUser.run(AddGroupsForAgentUser:41)
   at com.kms.katalon.core.main.ScriptEngine.run(ScriptEngine.java:194)
   at com.kms.katalon.core.main.ScriptEngine.runScriptAsRawText(ScriptEngine.java:119)
   at com.kms.katalon.core.main.TestCaseExecutor.runScript(TestCaseExecutor.java:448)
   at com.kms.katalon.core.main.TestCaseExecutor.doExecute(TestCaseExecutor.java:439)
   at com.kms.katalon.core.main.TestCaseExecutor.processExecutionPhase(TestCaseExecutor.java:418)
   at com.kms.katalon.core.main.TestCaseExecutor.accessMainPhase(TestCaseExecutor.java:410)
   at com.kms.katalon.core.main.TestCaseExecutor.execute(TestCaseExecutor.java:285)
   at com.kms.katalon.core.main.TestCaseMain.runTestCase(TestCaseMain.java:144)
   at com.kms.katalon.core.keyword.builtin.CallTestCaseKeyword$_callTestCase_closure1.doCall(CallTestCaseKeyword.groovy:59)
   at com.kms.katalon.core.keyword.builtin.CallTestCaseKeyword$_callTestCase_closure1.call(CallTestCaseKeyword.groovy)
   at com.kms.katalon.core.keyword.internal.KeywordMain.runKeyword(KeywordMain.groovy:75)
   at com.kms.katalon.core.keyword.builtin.CallTestCaseKeyword.callTestCase(CallTestCaseKeyword.groovy:81)
   at com.kms.katalon.core.keyword.builtin.CallTestCaseKeyword.execute(CallTestCaseKeyword.groovy:44)
   at com.kms.katalon.core.keyword.internal.KeywordExecutor.executeKeywordForPlatform(KeywordExecutor.groovy:74)
   at com.kms.katalon.core.keyword.BuiltinKeywords.callTestCase(BuiltinKeywords.groovy:310)
   at AddAgentUser.run(AddAgentUser:116)
   at com.kms.katalon.core.main.ScriptEngine.run(ScriptEngine.java:194)
   at com.kms.katalon.core.main.ScriptEngine.runScriptAsRawText(ScriptEngine.java:119)
   at com.kms.katalon.core.main.TestCaseExecutor.runScript(TestCaseExecutor.java:448)
   at com.kms.katalon.core.main.TestCaseExecutor.doExecute(TestCaseExecutor.java:439)
   at com.kms.katalon.core.main.TestCaseExecutor.processExecutionPhase(TestCaseExecutor.java:418)
   at com.kms.katalon.core.main.TestCaseExecutor.accessMainPhase(TestCaseExecutor.java:410)
   at com.kms.katalon.core.main.TestCaseExecutor.execute(TestCaseExecutor.java:285)
   at com.kms.katalon.core.main.TestCaseMain.runTestCase(TestCaseMain.java:144)
   at com.kms.katalon.core.keyword.builtin.CallTestCaseKeyword$_callTestCase_closure1.doCall(CallTestCaseKeyword.groovy:59)
   at com.kms.katalon.core.keyword.builtin.CallTestCaseKeyword$_callTestCase_closure1.call(CallTestCaseKeyword.groovy)
   at com.kms.katalon.core.keyword.internal.KeywordMain.runKeyword(KeywordMain.groovy:75)
   at com.kms.katalon.core.keyword.builtin.CallTestCaseKeyword.callTestCase(CallTestCaseKeyword.groovy:81)
   at com.kms.katalon.core.keyword.builtin.CallTestCaseKeyword.execute(CallTestCaseKeyword.groovy:44)
   at com.kms.katalon.core.keyword.internal.KeywordExecutor.executeKeywordForPlatform(KeywordExecutor.groovy:74)
   at com.kms.katalon.core.keyword.BuiltinKeywords.callTestCase(BuiltinKeywords.groovy:310)
   at Script1696492713218.SetUp(Script1696492713218.groovy:238)
   at com.kms.katalon.core.main.ScriptEngine.runScriptMethodAsRawText(ScriptEngine.java:133)
   at com.kms.katalon.core.main.TestCaseExecutor.runMethod(TestCaseExecutor.java:458)
   at com.kms.katalon.core.main.TestCaseExecutor.runMethod(TestCaseExecutor.java:580)
   at com.kms.katalon.core.main.TestCaseExecutor.internallyRunMethods(TestCaseExecutor.java:565)
   at com.kms.katalon.core.main.TestCaseExecutor.processSetupPhase(TestCaseExecutor.java:203)
   at com.kms.katalon.core.main.TestCaseExecutor.accessMainPhase(TestCaseExecutor.java:407)
   at com.kms.katalon.core.main.TestCaseExecutor.execute(TestCaseExecutor.java:285)
   at com.kms.katalon.core.common.CommonExecutor.accessTestCaseMainPhase(CommonExecutor.java:65)
   at com.kms.katalon.core.main.TestSuiteExecutor.accessTestSuiteMainPhase(TestSuiteExecutor.java:148)
   at com.kms.katalon.core.main.TestSuiteExecutor.execute(TestSuiteExecutor.java:106)
   at com.kms.katalon.core.main.TestCaseMain.startTestSuite(TestCaseMain.java:187)
   at com.kms.katalon.core.main.TestCaseMain$startTestSuite$0.call(Unknown Source)
   at TempTestSuite1698055196560.run(TempTestSuite1698055196560.groovy:36)
Caused by: org.openqa.selenium.StaleElementReferenceException: stale element reference: stale element not found
 (Session info: headless chrome=118.0.5993.71)
For documentation on this error, please visit: https://www.seleniumhq.org/exceptions/stale_element_reference.html
Build info: version: '3.141.59', revision: 'e82be7d358', time: '2018-11-14T08:25:53'
Driver info: org.openqa.selenium.chrome.ChromeDriver
Capabilities {acceptInsecureCerts: false, browserName: chrome, browserVersion: 118.0.5993.71, chrome: {chromedriverVersion: 118.0.5993.70 (e52f33f30b91..., userDataDir: C:\Users\Zahid\AppData\Loca...}, fedcm:accounts: true, goog:chromeOptions: {debuggerAddress: localhost:60544}, javascriptEnabled: true, networkConnectionEnabled: false, pageLoadStrategy: normal, platform: WINDOWS, platformName: WINDOWS, proxy: Proxy(), setWindowRect: true, strictFileInteractability: false, timeouts: {implicit: 0, pageLoad: 300000, script: 30000}, unhandledPromptBehavior: dismiss and notify, webauthn:extension:credBlob: true, webauthn:extension:largeBlob: true, webauthn:extension:minPinLength: true, webauthn:extension:prf: true, webauthn:virtualAuthenticators: true}
Session ID: 804c8af7723c50137ce4abdeb2e31fea
   at org.openqa.selenium.remote.http.W3CHttpResponseCodec.createException(W3CHttpResponseCodec.java:187)
   at org.openqa.selenium.remote.http.W3CHttpResponseCodec.decode(W3CHttpResponseCodec.java:122)
   at org.openqa.selenium.remote.http.W3CHttpResponseCodec.decode(W3CHttpResponseCodec.java:49)
   at org.openqa.selenium.remote.HttpCommandExecutor.execute(HttpCommandExecutor.java:158)
   at org.openqa.selenium.remote.service.DriverCommandExecutor.execute(DriverCommandExecutor.java:83)
   at org.openqa.selenium.remote.RemoteWebDriver.execute(RemoteWebDriver.java:552)
   at org.openqa.selenium.remote.RemoteWebElement.execute(RemoteWebElement.java:285)
   at org.openqa.selenium.remote.RemoteWebElement.click(RemoteWebElement.java:84)
   at org.openqa.selenium.support.events.EventFiringWebDriver$EventFiringWebElement.lambda$new$0(EventFiringWebDriver.java:404)
   at com.sun.proxy.$Proxy13.click(Unknown Source)
   at org.openqa.selenium.support.events.EventFiringWebDriver$EventFiringWebElement.click(EventFiringWebDriver.java:417)
   at org.openqa.selenium.WebElement$click.call(Unknown Source)
   at com.kms.katalon.core.webui.keyword.builtin.ClickKeyword.clickUntilSuccessWithTimeout(ClickKeyword.groovy:81)
   at com.kms.katalon.core.webui.keyword.builtin.ClickKeyword.clickUntilSuccessWithTimeout(ClickKeyword.groovy)
   at com.kms.katalon.core.webui.keyword.builtin.ClickKeyword$_click_closure1.doCall(ClickKeyword.groovy:67)
   at com.kms.katalon.core.webui.keyword.builtin.ClickKeyword$_click_closure1.call(ClickKeyword.groovy)
   at com.kms.katalon.core.webui.keyword.internal.WebUIKeywordMain.runKeyword(WebUIKeywordMain.groovy:20)
   at com.kms.katalon.core.webui.keyword.builtin.ClickKeyword.click(ClickKeyword.groovy:74)
   at com.kms.katalon.core.webui.keyword.builtin.ClickKeyword.execute(ClickKeyword.groovy:40)
   at com.kms.katalon.core.keyword.internal.KeywordExecutor.executeKeywordForPlatform(KeywordExecutor.groovy:74)
   at com.kms.katalon.core.webui.keyword.WebUiBuiltInKeywords.click(WebUiBuiltInKeywords.groovy:620)
   at com.kms.katalon.core.webui.keyword.WebUiBuiltInKeywords$click$16.call(Unknown Source)
   at Script1685514346063.run(Script1685514346063.groovy:41)
   at com.kms.katalon.core.main.ScriptEngine.run(ScriptEngine.java:194)
   at com.kms.katalon.core.main.ScriptEngine.runScriptAsRawText(ScriptEngine.java:119)
   at com.kms.katalon.core.main.TestCaseExecutor.runScript(TestCaseExecutor.java:448)
   at com.kms.katalon.core.main.TestCaseExecutor.doExecute(TestCaseExecutor.java:439)
   at com.kms.katalon.core.main.TestCaseExecutor.processExecutionPhase(TestCaseExecutor.java:418)
   at com.kms.katalon.core.main.TestCaseExecutor.accessMainPhase(TestCaseExecutor.java:410)
   at com.kms.katalon.core.main.TestCaseExecutor.execute(TestCaseExecutor.java:285)
   at com.kms.katalon.core.main.TestCaseMain.runTestCase(TestCaseMain.java:144)
   at com.kms.katalon.core.keyword.builtin.CallTestCaseKeyword$_callTestCase_closure1.doCall(CallTestCaseKeyword.groovy:59)
   at com.kms.katalon.core.keyword.builtin.CallTestCaseKeyword$_callTestCase_closure1.call(CallTestCaseKeyword.groovy)
   at com.kms.katalon.core.keyword.internal.KeywordMain.runKeyword(KeywordMain.groovy:75)
   at com.kms.katalon.core.keyword.builtin.CallTestCaseKeyword.callTestCase(CallTestCaseKeyword.groovy:81)
   at com.kms.katalon.core.keyword.builtin.CallTestCaseKeyword.execute(CallTestCaseKeyword.groovy:44)
   at com.kms.katalon.core.keyword.internal.KeywordExecutor.executeKeywordForPlatform(KeywordExecutor.groovy:74)
   at com.kms.katalon.core.keyword.BuiltinKeywords.callTestCase(BuiltinKeywords.groovy:310)
   at Script1661755082722.run(Script1661755082722.groovy:116)
   ... 29 more
)                        
          

As you can see from above logs, before this exception happened, verifyElementVisible action is passed which I beleive shows that the web element “btn_JoinGroups” is still attached with DOM and also it is visible to make a click on it.

At which point you are getting the exception

It usually (not very common though) happened when script tried to make a click using

WebUI.click(findTestObject('Keycloak_Objects/Users/AddUsers/Groups/btn_JoinGroups'))

Is this accurate?

My steps are as

- Add user
- Click on credentials tab
--- Set credentials
- Click on role mapping tab
--- Assign role
- Click on groups tab
--- Click on Join button

one quick fix might be to use Smart Wait. Do you have this enabled in your project?

Yes, it is already enabled for this project.

Looking forward for your reply…

Hi @zahid.asim, this might help: Advice for Element Not Interactable Exception for offscreen objects? - #2 by Dave_Evers

Right, so you could try catching the top level exception instead, which is this:

com.kms.katalon.core.exception.StepFailedException

You should also try setting the failure handling argument on WebUI.click():

WebUI.click(findTestObject('Keycloak_Objects/Users/AddUsers/Groups/btn_JoinGroups'), FailureHandling.CONTINUE_ON_FAILURE)

StaleElementReferenceExceptions always occur intermittently. They are indicative of a timing issue. Ultimately, you need a better wait condition between clicking the Groups tab and clicking the Join Groups button. It looks like you’re working with a different version of Keycloak than I am, so it makes sense that my wait condition might not work for you.

Is there any sort of loading bar/overlay/spinner anywhere in the Keycloak app after you click the Groups tab?

I will give it a try and udpate here.

No, not really