{"record":{"id":"4adf7fa5aecf37d4","repo":"karatelabs/karate","slug":"timeout-waiting-for-text-expected-in-element-locator","errorCode":null,"errorMessage":"timeout waiting for text '${expected}' in element: ${locator}","messagePattern":"timeout waiting for text '(.+?)' in element: (.+?)","errorType":"exception","errorClass":"DriverException","httpStatus":null,"severity":"error","filePath":"karate-core/src/main/java/io/karatelabs/driver/cdp/CdpDriver.java","lineNumber":3350,"sourceCode":"    public Element waitForText(String locator, String expected) {\n        return waitForText(locator, expected, options.getTimeoutDuration());\n    }\n\n    /**\n     * Wait for an element to contain specific text with custom timeout.\n     */\n    public Element waitForText(String locator, String expected, Duration timeout) {\n        Element found = pollFor(timeout.toMillis(), options.getRetryInterval(), () -> {\n            if (exists(locator)) {\n                String text = text(locator);\n                if (text != null && text.contains(expected)) {\n                    return BaseElement.existing(this, locator);\n                }\n            }\n            return null;\n        });\n        if (found == null) {\n            throw new DriverException(\"timeout waiting for text '\" + expected + \"' in element: \" + locator);\n        }\n        return found;\n    }\n\n    /**\n     * Wait for an element to be enabled.\n     */\n    public Element waitForEnabled(String locator) {\n        return waitForEnabled(locator, options.getTimeoutDuration());\n    }\n\n    /**\n     * Wait for an element to be enabled with custom timeout.\n     */\n    public Element waitForEnabled(String locator, Duration timeout) {\n        Element found = pollFor(timeout.toMillis(), options.getRetryInterval(),\n                () -> exists(locator) && enabled(locator) ? BaseElement.existing(this, locator) : null);\n        if (found == null) {","sourceCodeStart":3332,"sourceCodeEnd":3368,"githubUrl":"https://github.com/karatelabs/karate/blob/a22eb90246d958d15a47bf436693d0121ad2812d/karate-core/src/main/java/io/karatelabs/driver/cdp/CdpDriver.java#L3332-L3368","documentation":"driver.waitForText(locator, expected, timeout) polls the element's text until it contains the expected string. If the timeout elapses without the text appearing, Karate throws this DriverException naming the expected text and the locator.","triggerScenarios":"driver.waitForText / * waitForText step where the element exists but its text never contains 'expected' within the timeout — async data never loaded, text differs in case/whitespace, or the expected value is stale.","commonSituations":"Backend slower than the timeout (slow API, cold cache); expectation out of date after copy changes; text rendered in a child element the polling selector does not cover; localized text differing from the hardcoded English expectation; waiting inside the wrong frame.","solutions":["Log or assert the element's actual text on failure to compare with the expectation.","Increase the timeout for slow async updates, or trigger/await the underlying request completion first.","Match the exact current copy including case/whitespace, or relax to a stable substring.","Ensure the correct frame/document is active and the selector targets the element that actually carries the text."],"exampleFix":"// before\ndriver.waitForText('#status', 'Complete', Duration.ofSeconds(5)); // app shows 'Completed'\n// after\ndriver.waitForText('#status', 'Complete', Duration.ofSeconds(30)); // or match 'Complete' substring actually present","handlingStrategy":"try-catch","validationCode":"// snapshot current text on failure for diagnosis\nString actual = driver.exists(locator) ? driver.text(locator) : \"<absent>\";","typeGuard":null,"tryCatchPattern":"try {\n    driver.waitForText(locator, expected, Duration.ofSeconds(30));\n} catch (Exception e) {\n    if (e.getMessage().contains(\"timeout waiting for text\")) {\n        String actual = driver.exists(locator) ? driver.text(locator) : \"<absent>\";\n        throw new AssertionError(\"expected text '\" + expected + \"' but saw: \" + actual, e);\n    } else throw e;\n}","preventionTips":["Wait on the element whose text actually changes.","Assert stable substrings rather than full sentences prone to copy edits.","Account for localization in expected values.","Increase timeouts for backend-driven text updates."],"tags":["driver","wait","timeout","text-assertion"],"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"}