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

  1. Log locateAll(locator).size() to see the actual count and adjust the expectation or selector
  2. Increase the timeout for slow data loads
  3. Assert the data source (API response) actually returned the expected number of items
  4. 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

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.

Related errors


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)