To resolve the WebElementNotFoundException in Katalon when an element works in one test but fails in others:
1. Fix Locator Issues
Check for Dynamic IDs
The error //a[@id='LiverpoolTheme_wt9_block_wtMenu...']
suggests a dynamic ID (e.g., auto-generated by frameworks like GWT). These IDs change across sessions, so avoid using them. Instead:
- Use a relative XPath/CSS selector with stable attributes (e.g.,
text
, class
, data-testid
):
groovy
//a[contains(@class, 'menu-item') and text()='Home']
- Re-spy the element in Katalon Studio to confirm if the ID has changed.
- Right-click the object → Update Selected.
Verify Object Repository Path
Ensure the object’s path in the Object Repository matches the current page structure.
- Example:
Incorrect Path: Page_OldName
Correct Path: Page_NewName
2. Handle Timing/Page Load Issues
Add explicit waits to ensure the element is loaded:
groovy
WebUI.waitForElementPresent(findTestObject('Object Repository/Page/YourElement'), 30)
WebUI.click(findTestObject('Object Repository/Page/YourElement'))
3. Check for Context Changes
If the page opens in a new tab/iframe and the context isn’t switched:
groovy
WebUI.switchToWindowTitle('Page Title')
groovy
WebUI.switchToFrame(findTestObject('Object Repository/iframe'), 10)
// Perform actions
WebUI.switchToDefaultContent() // Exit iframe
4. Debugging Steps
Run in Debug Mode
Set breakpoints and step through the test to inspect the page state when the error occurs.
Capture Screenshots
Add screenshots before the failure:
groovy
WebUI.takeScreenshot()
Check Network/Console
Add code to log browser console errors:
groovy
String consoleLog = WebUI.executeJavaScript('return JSON.stringify(console.log)', null)
println "Console Log: $consoleLog"
5. Optimize Katalon Settings
- Increase Timeout: Go to Settings > WebUI > Default Timeout (set to 30+ seconds).
- Disable Smart Wait: In some cases,
Settings > WebUI > Enable SmartWait
can conflict with dynamic content.
6. Page URL/Flow Consistency
Ensure the test navigates to the correct URL and follows the same steps:
groovy
// Navigate explicitly
WebUI.navigateToUrl('https://your-app.com/page')
7. Update Dependencies
- Upgrade Katalon to the latest version.
- Reinstall or update the Multi-Level Shadow DOM Plugin if shadow DOM is involved.
Example Fix
Before (using dynamic ID):
groovy
WebUI.click(findTestObject('Object Repository/Page_/DynamicIdElement'))
After (using stable attributes):
groovy
// Object defined with XPath: //a[contains(text(), 'Submit')]
WebUI.waitForElementPresent(findTestObject('Object Repository/Page_/SubmitButton'), 30)
WebUI.click(findTestObject('Object Repository/Page_/SubmitButton'))