karatelabs/karate · error · DriverException
timeout waiting for elements
Error message
timeout waiting for ${count} elements: ${locator} What it means
CdpDriver.waitForResultCount(locator, count, timeout) polls a JS count of elements matching the locator until it equals the requested count, then returns locateAll(locator); on timeout a DriverException reports the expected count and locator. Use it to wait for a specific number of matches (e.g. rows loaded).
Solutions
- Log locateAll(locator).size() to see the actual count and adjust the expectation or selector
- Increase the timeout for slow data loads
- Assert the data source (API response) actually returned the expected number of items
- Wait for a stable subset (e.g. waitForResultCount(locator, 1, ...)) plus a DOM-settled signal instead of an exact dynamic count
Example fix
// before
driver.waitForResultCount("table tr", 25, Duration.ofSeconds(5));
// after
List<Element> rows = driver.locateAll("table tbody tr");
logger.debug("rows={}", rows.size());
driver.waitForResultCount("table tbody tr", rows.isEmpty() ? 10 : rows.size(), Duration.ofSeconds(15)); Defensive patterns
Strategy: validation
Validate before calling
int actual = driver.locateAll("table tbody tr").size();
logger.debug("current row count={}", actual); // compare against expected before waiting Try / catch
try {
driver.waitForResultCount("table tbody tr", expectedRows, Duration.ofSeconds(20));
} catch (DriverException e) {
int actual = driver.locateAll("table tbody tr").size();
logger.error("expected {} rows, found {}", expectedRows, actual);
throw e;
} Prevention
- Scope selectors tightly (tbody tr) so hidden/template nodes don't inflate the count
- Don't hardcode dynamic data sizes; derive expected counts from the data source
- Log actual count on failure for fast diagnosis
When it happens
Trigger: waitForResultCount(".row", 10, duration) when fewer/more elements match than expected at timeout — list never fully loads, pagination renders different counts, or duplicate selectors match extra nodes.
Common situations: Infinite scroll lists that never reach the expected count; data fetch failed so zero rows render; CSS selector also matches hidden/template nodes so the count overshoots; expected count hardcoded while data is dynamic.
Understand the failure class
Background: Request timed out: what client-side request timeouts mean across libraries (Request timed out, TIMED_OUT, APITimeoutError) — this error's family across 39 libraries.
- Timeouts: ETIMEDOUT, deadlines, and hung requests — what actually expires when a request times out.
Related errors
- timeout waiting for element
- timeout waiting for any element
- timeout waiting for text
- timeout waiting for element to be enabled
- waitForResultCount timeout
AI-assisted analysis of karatelabs/karate@a22eb90246 (2026-09-12).
Data as JSON: /api/errors/8c57332f5f63a7b1.
Report an issue: GitHub.
Appendix: source
Thrown at karate-core/src/main/java/io/karatelabs/driver/cdp/CdpDriver.java:3470
}
return result;
}
/**
* Wait for a specific number of elements to match.
*/
public List<Element> waitForResultCount(String locator, int count) {
return waitForResultCount(locator, count, options.getTimeoutDuration());
}
/**
* Wait for a specific number of elements to match with custom timeout.
*/
public List<Element> waitForResultCount(String locator, int count, Duration timeout) {
boolean met = pollUntil(timeout.toMillis(), options.getRetryInterval(),
() -> ((Number) script(Locators.countJs(locator))).intValue() == count);
if (!met) {
throw new DriverException("timeout waiting for " + count + " elements: " + locator);
}
return locateAll(locator);
}
// ========== Cookies ==========
/**
* Get a cookie by name.
*
* @param name the cookie name
* @return the cookie as a Map, or null if not found
*/
@SuppressWarnings("unchecked")
public Map<String, Object> cookie(String name) {
CdpResponse response = cdp.method("Network.getCookies").send();
List<Map<String, Object>> cookies = response.getResult("cookies");
if (cookies != null) {
for (Map<String, Object> cookie : cookies) {View on GitHub (pinned to a22eb90246)