{"record":{"id":"8c57332f5f63a7b1","repo":"karatelabs/karate","slug":"timeout-waiting-for-count-elements-locator","errorCode":null,"errorMessage":"timeout waiting for ${count} elements: ${locator}","messagePattern":"timeout waiting for (.+?) elements: (.+?)","errorType":"exception","errorClass":"DriverException","httpStatus":null,"severity":"error","filePath":"karate-core/src/main/java/io/karatelabs/driver/cdp/CdpDriver.java","lineNumber":3470,"sourceCode":"        }\n        return result;\n    }\n\n    /**\n     * Wait for a specific number of elements to match.\n     */\n    public List<Element> waitForResultCount(String locator, int count) {\n        return waitForResultCount(locator, count, options.getTimeoutDuration());\n    }\n\n    /**\n     * Wait for a specific number of elements to match with custom timeout.\n     */\n    public List<Element> waitForResultCount(String locator, int count, Duration timeout) {\n        boolean met = pollUntil(timeout.toMillis(), options.getRetryInterval(),\n                () -> ((Number) script(Locators.countJs(locator))).intValue() == count);\n        if (!met) {\n            throw new DriverException(\"timeout waiting for \" + count + \" elements: \" + locator);\n        }\n        return locateAll(locator);\n    }\n\n    // ========== Cookies ==========\n\n    /**\n     * Get a cookie by name.\n     *\n     * @param name the cookie name\n     * @return the cookie as a Map, or null if not found\n     */\n    @SuppressWarnings(\"unchecked\")\n    public Map<String, Object> cookie(String name) {\n        CdpResponse response = cdp.method(\"Network.getCookies\").send();\n        List<Map<String, Object>> cookies = response.getResult(\"cookies\");\n        if (cookies != null) {\n            for (Map<String, Object> cookie : cookies) {","sourceCodeStart":3452,"sourceCodeEnd":3488,"githubUrl":"https://github.com/karatelabs/karate/blob/a22eb90246d958d15a47bf436693d0121ad2812d/karate-core/src/main/java/io/karatelabs/driver/cdp/CdpDriver.java#L3452-L3488","documentation":"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).","triggerScenarios":"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.","commonSituations":"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.","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"],"exampleFix":"// before\ndriver.waitForResultCount(\"table tr\", 25, Duration.ofSeconds(5));\n// after\nList<Element> rows = driver.locateAll(\"table tbody tr\");\nlogger.debug(\"rows={}\", rows.size());\ndriver.waitForResultCount(\"table tbody tr\", rows.isEmpty() ? 10 : rows.size(), Duration.ofSeconds(15));","handlingStrategy":"validation","validationCode":"int actual = driver.locateAll(\"table tbody tr\").size();\nlogger.debug(\"current row count={}\", actual); // compare against expected before waiting","typeGuard":null,"tryCatchPattern":"try {\n    driver.waitForResultCount(\"table tbody tr\", expectedRows, Duration.ofSeconds(20));\n} catch (DriverException e) {\n    int actual = driver.locateAll(\"table tbody tr\").size();\n    logger.error(\"expected {} rows, found {}\", expectedRows, actual);\n    throw e;\n}","preventionTips":["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"],"tags":["driver","timeout","element-count","wait"],"backgroundTag":"request-timeout","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"}