{"record":{"id":"baab44fb950c8642","repo":"karatelabs/karate","slug":"could-not-find-active-element-for-keyboard-input","errorCode":null,"errorMessage":"Could not find active element for keyboard input","messagePattern":"Could not find active element for keyboard input","errorType":"exception","errorClass":"RuntimeException","httpStatus":null,"severity":"error","filePath":"karate-core/src/main/java/io/karatelabs/driver/w3c/W3cKeys.java","lineNumber":186,"sourceCode":"        keyboard.put(\"id\", \"keyboard\");\n        keyboard.put(\"actions\", keyActions);\n        session.performActions(List.of(keyboard));\n    }\n\n    private static Map<String, Object> keyDown(String value) {\n        return Map.of(\"type\", \"keyDown\", \"value\", value);\n    }\n\n    private static Map<String, Object> keyUp(String value) {\n        return Map.of(\"type\", \"keyUp\", \"value\", value);\n    }\n\n    private String findActiveElement() {\n        Object result = session.executeScript(\"return document.activeElement\");\n        if (W3cSession.isElementReference(result)) {\n            return W3cSession.elementIdFrom(result);\n        }\n        throw new RuntimeException(\"Could not find active element for keyboard input\");\n    }\n\n}\n","sourceCodeStart":168,"sourceCodeEnd":190,"githubUrl":"https://github.com/karatelabs/karate/blob/a22eb90246d958d15a47bf436693d0121ad2812d/karate-core/src/main/java/io/karatelabs/driver/w3c/W3cKeys.java#L168-L190","documentation":"W3cKeys.findActiveElement runs `return document.activeElement` via executeScript and throws RuntimeException if the result is not a valid WebDriver element reference. Keyboard actions (key press/send) target the currently focused element, so without an active element the input cannot be delivered.","triggerScenarios":"Calling keyboard input APIs (driver.input via keys, keyUp/keyDown, press) when document.activeElement is null or not focusable — e.g. focus is on <body>/no element, or the script ran in a context without a document element reference.","commonSituations":"Sending keys right after navigation or page load before any element has focus; trying to type into a page where the target input was never clicked/focused; acting inside an iframe whose document has no active element.","solutions":["Click or explicitly focus the target element before sending keys (element.focus() or driver.click)","Wait for the page to finish loading so an interactive element gains focus","Switch to the correct iframe/window before keyboard input"],"exampleFix":"// before\ndriver.keys(\"hello\"); // no focused element\n// after\ndriver.input(\"#search-box\", \"hello\"); // focuses and types into a concrete element","handlingStrategy":"validation","validationCode":"// ensure something has focus before keyboard input\n// e.g. driver.input(targetSelector, value) or click the element first","typeGuard":null,"tryCatchPattern":"try {\n    driver.keys(text);\n} catch (RuntimeException e) {\n    if (e.getMessage().contains(\"Could not find active element\")) {\n        driver.click(targetSelector); // establish focus, then retry\n        driver.keys(text);\n    }\n}","preventionTips":["Focus (click or .focus()) an element before raw key input","Prefer element-scoped input() over global keyboard events","After navigation, wait for load and re-establish focus"],"tags":["webdriver","keyboard-input","focus","active-element"],"backgroundTag":"resource-not-found","analyzedSha":"a22eb90246d958d15a47bf436693d0121ad2812d","analyzedAt":"2026-09-12T09:01:00.220Z","contentChangedAt":"2026-09-12T09:01:00.220Z","schemaVersion":2},"datasetVersion":"2026-09-19T12:17:13.211Z"}