{"record":{"id":"54256b8e1311266b","repo":"karatelabs/karate","slug":"timeout-waiting-for-condition-expression-on-element-locator","errorCode":null,"errorMessage":"timeout waiting for condition '${expression}' on element: ${locator}","messagePattern":"timeout waiting for condition '(.+?)' on element: (.+?)","errorType":"exception","errorClass":"DriverException","httpStatus":null,"severity":"error","filePath":"karate-core/src/main/java/io/karatelabs/driver/cdp/CdpDriver.java","lineNumber":3411,"sourceCode":"    }\n\n    /**\n     * Wait until a JavaScript expression on an element evaluates to truthy.\n     * The element is available as '_' in the expression.\n     */\n    public Element waitUntil(String locator, String expression) {\n        return waitUntil(locator, expression, options.getTimeoutDuration());\n    }\n\n    /**\n     * Wait until a JavaScript expression on an element evaluates to truthy.\n     */\n    public Element waitUntil(String locator, String expression, Duration timeout) {\n        Element found = pollFor(timeout.toMillis(), options.getRetryInterval(),\n                () -> exists(locator) && Terms.isTruthy(script(locator, expression))\n                        ? BaseElement.existing(this, locator) : null);\n        if (found == null) {\n            throw new DriverException(\"timeout waiting for condition '\" + expression + \"' on element: \" + locator);\n        }\n        return found;\n    }\n\n    /**\n     * Wait until a JavaScript expression evaluates to truthy.\n     */\n    public boolean waitUntil(String expression) {\n        return waitUntil(expression, options.getTimeoutDuration());\n    }\n\n    /**\n     * Wait until a JavaScript expression evaluates to truthy with custom timeout.\n     */\n    public boolean waitUntil(String expression, Duration timeout) {\n        boolean met = pollUntil(timeout.toMillis(), options.getRetryInterval(),\n                () -> Terms.isTruthy(script(expression)));\n        if (!met) {","sourceCodeStart":3393,"sourceCodeEnd":3429,"githubUrl":"https://github.com/karatelabs/karate/blob/a22eb90246d958d15a47bf436693d0121ad2812d/karate-core/src/main/java/io/karatelabs/driver/cdp/CdpDriver.java#L3393-L3429","documentation":"CdpDriver.waitUntil(locator, expression, timeout) polls until the element exists AND the given JavaScript expression evaluated on that element is truthy; otherwise a DriverException naming the expression and locator is thrown. It is the generic per-element condition wait.","triggerScenarios":"driver.waitUntil(locator, expression, duration) where the element is missing, or the JS expression (e.g. \"_.value != ''\", \"arguments[0].classList.contains('ready')\") keeps evaluating false/throws in the page until the timeout elapses.","commonSituations":"Expression syntax not valid in the page context (returns undefined rather than true); waiting on a state a slow backend never delivers; wrong locator so script() fails silently each poll; app never reaches the expected state because of a bug or failed pre-step.","solutions":["Test the expression directly with driver.script(locator, expression) and log the result to see what the page actually returns","Verify the locator resolves and the expression returns a truthy JS value (true, non-empty string, non-zero number)","Increase the timeout for genuinely slow async state","Fix the preconditions (fill data, trigger event) so the condition can ever become true"],"exampleFix":"// before\ndriver.waitUntil(\"#status\", \"_.innerText == 'Done'\", Duration.ofSeconds(3));\n// after\nString txt = (String) driver.script(\"#status\", \"_.innerText\");\n// log txt, correct expectation or timeout, e.g.:\ndriver.waitUntil(\"#status\", \"_.innerText.trim() == 'Done'\", Duration.ofSeconds(15));","handlingStrategy":"validation","validationCode":"// sanity-check the expression in-page before waiting\nObject val = driver.script(\"#status\", \"_.innerText\");\nlogger.debug(\"current status={}\", val);","typeGuard":null,"tryCatchPattern":"try {\n    driver.waitUntil(\"#status\", \"_.innerText.trim() == 'Done'\", Duration.ofSeconds(15));\n} catch (DriverException e) {\n    Object cur = driver.script(\"#status\", \"_.innerText\");\n    logger.error(\"condition not met; current value={}\", cur);\n    throw e;\n}","preventionTips":["Test the JS expression in browser devtools before embedding it","Make expressions null-safe (optional chaining) so they return false instead of throwing","Prefer boolean-producing expressions (=== true) over truthy guessing","Use generous timeouts for backend-dependent state"],"tags":["driver","timeout","javascript","wait-until"],"backgroundTag":"request-timeout","analyzedSha":"a22eb90246d958d15a47bf436693d0121ad2812d","analyzedAt":"2026-09-12T09:01:00.220Z","contentChangedAt":"2026-09-12T09:01:00.220Z","schemaVersion":2},"datasetVersion":"2026-09-19T12:17:13.211Z"}