{"record":{"id":"6df13788bf3fda87","repo":"karatelabs/karate","slug":"waitforresultcount-timeout-locator-expected-count-count","errorCode":null,"errorMessage":"waitForResultCount timeout: ${locator} expected count: ${count}","messagePattern":"waitForResultCount timeout: (.+?) expected count: (.+?)","errorType":"exception","errorClass":"DriverException","httpStatus":null,"severity":"error","filePath":"karate-core/src/main/java/io/karatelabs/driver/w3c/W3cDriver.java","lineNumber":695,"sourceCode":"        throw new DriverException(\"waitUntil (supplier) timeout\");\n    }\n\n    @Override\n    public List<Element> waitForResultCount(String locator, int count) {\n        return waitForResultCount(locator, count, options.getTimeoutDuration());\n    }\n\n    @Override\n    public List<Element> waitForResultCount(String locator, int count, java.time.Duration timeout) {\n        long deadline = System.currentTimeMillis() + timeout.toMillis();\n        while (System.currentTimeMillis() < deadline) {\n            List<Element> results = locateAll(locator);\n            if (results.size() == count) {\n                return results;\n            }\n            sleep(options.getRetryInterval());\n        }\n        throw new DriverException(\"waitForResultCount timeout: \" + locator + \" expected count: \" + count);\n    }\n\n    // ========== Navigation ==========\n\n    @Override\n    public String getUrl() {\n        return session.getUrl();\n    }\n\n    @Override\n    public String getTitle() {\n        return session.getTitle();\n    }\n\n    // ========== Cookies ==========\n\n    @Override\n    public void deleteCookie(String name) {","sourceCodeStart":677,"sourceCodeEnd":713,"githubUrl":"https://github.com/karatelabs/karate/blob/a22eb90246d958d15a47bf436693d0121ad2812d/karate-core/src/main/java/io/karatelabs/driver/w3c/W3cDriver.java#L677-L713","documentation":"W3cDriver.waitForResultCount polls locateAll(locator) every retry interval and returns only when the matched element count exactly equals the expected count; otherwise, after the timeout, it throws a DriverException naming the locator and expected count.","triggerScenarios":"Calling driver.waitForResultCount(locator, count) when the number of matched elements never equals count — too few (list still loading, filter not applied) or too many (duplicates, selector too broad) within the timeout.","commonSituations":"Waiting for a table to show exactly N rows but pagination/lazy-loading changes the count; selector matches hidden template nodes inflating the count; expected count off by one due to a header row; async data fetch slower than timeout.","solutions":["Log locateAll(locator).size() to see the actual count and compare with expected","Refine the selector (scope, :visible-style filters) so it matches exactly the intended nodes","Increase the timeout via waitForResultCount(locator, count, timeout)","If exact equality is too strict, poll and assert a >= or <= condition instead"],"exampleFix":"// before\ndriver.waitForResultCount('table tr', 5); // header row makes it 6\n// after\ndriver.waitForResultCount('table tbody tr', 5);","handlingStrategy":"validation","validationCode":"int actual = driver.locateAll(locator).size();\nif (actual != expected) { /* adjust selector or expected count */ }","typeGuard":null,"tryCatchPattern":"try {\n    List<Element> rows = driver.waitForResultCount(locator, count, timeout);\n} catch (DriverException e) {\n    // log driver.locateAll(locator).size() to compare actual vs expected\n}","preventionTips":["Scope selectors (tbody, container id) to avoid counting headers/templates","Verify final rendered count in the browser before hardcoding it","Prefer a range/predicate when exact equality is not the real requirement"],"tags":["webdriver","timeout","wait","element-count"],"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"}