karatelabs/karate · error · RuntimeException
Could not find active element for keyboard input
Error message
Could not find active element for keyboard input
What it means
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.
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
Example fix
// before
driver.keys("hello"); // no focused element
// after
driver.input("#search-box", "hello"); // focuses and types into a concrete element Defensive patterns
Strategy: validation
Validate before calling
// ensure something has focus before keyboard input // e.g. driver.input(targetSelector, value) or click the element first
Try / catch
try {
driver.keys(text);
} catch (RuntimeException e) {
if (e.getMessage().contains("Could not find active element")) {
driver.click(targetSelector); // establish focus, then retry
driver.keys(text);
}
} Prevention
- 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
When it happens
Trigger: 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.
Common situations: 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.
Understand the failure class
Background: 'Could not be found', 'does not exist', 'not found in database': the resource-not-found family when an ID, slug, key, or URI lookup comes back empty — this error's family across 20 libraries.
Related errors
- Dialog callback handler not supported on WebDriver backend…
- Element not found
- Error closing driver
- Failed to start
- javascript failed
AI-assisted analysis of karatelabs/karate@a22eb90246 (2026-09-12).
Data as JSON: /api/errors/baab44fb950c8642.
Report an issue: GitHub.
Appendix: source
Thrown at karate-core/src/main/java/io/karatelabs/driver/w3c/W3cKeys.java:186
keyboard.put("id", "keyboard");
keyboard.put("actions", keyActions);
session.performActions(List.of(keyboard));
}
private static Map<String, Object> keyDown(String value) {
return Map.of("type", "keyDown", "value", value);
}
private static Map<String, Object> keyUp(String value) {
return Map.of("type", "keyUp", "value", value);
}
private String findActiveElement() {
Object result = session.executeScript("return document.activeElement");
if (W3cSession.isElementReference(result)) {
return W3cSession.elementIdFrom(result);
}
throw new RuntimeException("Could not find active element for keyboard input");
}
}
View on GitHub (pinned to a22eb90246)