{"record":{"id":"bcc697f72a4a38e1","repo":"karatelabs/karate","slug":"element-not-found","errorCode":null,"errorMessage":"element not found: ","messagePattern":"element not found: ","errorType":"exception","errorClass":"DriverException","httpStatus":null,"severity":"error","filePath":"karate-core/src/main/java/io/karatelabs/driver/BaseElement.java","lineNumber":383,"sourceCode":"                    }\n                }\n                return inputFile(files.toArray(new String[0]));\n            };\n            case \"attribute\" -> (JavaCallable) (ctx, args) -> attribute(args.length > 0 ? String.valueOf(args[0]) : \"\");\n            case \"property\" -> (JavaCallable) (ctx, args) -> property(args.length > 0 ? String.valueOf(args[0]) : \"\");\n            case \"script\" -> (JavaCallable) (ctx, args) -> script(args.length > 0 ? String.valueOf(args[0]) : \"\");\n            // Navigation — selector-based, the W3C DOM Element idioms.\n            case \"closest\" -> (JavaCallable) (ctx, args) -> closest(args.length > 0 ? String.valueOf(args[0]) : \"\");\n            case \"matches\" -> (JavaCallable) (ctx, args) -> matches(args.length > 0 ? String.valueOf(args[0]) : \"\");\n            default -> null;\n        };\n    }\n\n    // ========== Utilities ==========\n\n    protected void assertExists() {\n        if (!exists) {\n            throw new DriverException(\"element not found: \" + locator);\n        }\n    }\n\n    @Override\n    public String toString() {\n        return \"Element[\" + locator + \", exists=\" + exists + \"]\";\n    }\n\n    // ========== Retry Element ==========\n\n    private static class RetryElement extends BaseElement {\n        private final Integer retryCount;\n        private final Integer retryInterval;\n\n        RetryElement(Driver driver, String locator, boolean exists) {\n            super(driver, locator, exists);\n            this.retryCount = null;\n            this.retryInterval = null;","sourceCodeStart":365,"sourceCodeEnd":401,"githubUrl":"https://github.com/karatelabs/karate/blob/a22eb90246d958d15a47bf436693d0121ad2812d/karate-core/src/main/java/io/karatelabs/driver/BaseElement.java#L365-L401","documentation":"BaseElement.assertExists() guards all element-content accessors (text, html, innerHtml, value, attribute, property). When the element does not exist in the DOM (exists == false), reading it throws this DriverException carrying the original locator. Karate fails fast instead of returning null/empty so the scenario stops at the real cause: the element was not there.","triggerScenarios":"Calling el.text() / el.html() / el.value() etc. on an Element returned by a lookup that failed to find the node — e.g. after driver.locate(\"#missing\"), or where a prior wait timed out and the stale Element handle is used.","commonSituations":"Wrong or outdated selector (CSS/XPath typo); element rendered only under certain conditions (modal, lazy load, auth state); race conditions where the script reads before the SPA renders; iframe/switch context misses.","solutions":["Wait for the element before reading: use driver.waitFor(\"#id\") or waitForText/location before calling accessors","Verify the locator (open the page devtools and test the CSS/XPath); fix typos and stale selectors","If absence is legitimate, use the optional variant (e.g. driver.optional(locator)) and check exists() before reading"],"exampleFix":"// before\nString txt = driver.locate(\"#result\").text();\n// after\ndriver.waitFor(\"#result\");\nString txt = driver.locate(\"#result\").text();","handlingStrategy":"try-catch","validationCode":"// in Karate script, before reading:\n// waitFor('#result') first; or use driver.optional('#result') and check exists()","typeGuard":"// if (driver.optional('#result').exists()) { /* safe to read */ }","tryCatchPattern":"try { String t = el.text(); } catch (DriverException e) { if (e.getMessage().startsWith(\"element not found\")) { /* handle absence */ } else throw e; }","preventionTips":["Always waitFor an element before reading it","Use driver.optional() when absence is a legitimate outcome","Keep selectors in sync with the app; re-verify after UI changes","Add waits for SPA/lazy-rendered content"],"tags":["ui","driver","dom","element-not-found"],"backgroundTag":"element-not-found","analyzedSha":"a22eb90246d958d15a47bf436693d0121ad2812d","analyzedAt":"2026-09-12T09:01:00.220Z","contentChangedAt":"2026-09-12T09:01:00.220Z","schemaVersion":2},"datasetVersion":"2026-09-16T19:17:19.609Z"}