{"record":{"id":"bc4b3df6a8c1348b","repo":"karatelabs/karate","slug":"timeout-waiting-for-any-element-locators","errorCode":null,"errorMessage":"timeout waiting for any element: ${locators}","messagePattern":"timeout waiting for any element: (.+?)","errorType":"exception","errorClass":"DriverException","httpStatus":null,"severity":"error","filePath":"karate-core/src/main/java/io/karatelabs/driver/cdp/CdpDriver.java","lineNumber":3324,"sourceCode":"     */\n    public Element waitForAny(String[] locators) {\n        return waitForAny(locators, options.getTimeoutDuration());\n    }\n\n    /**\n     * Wait for any of the locators to match with custom timeout.\n     */\n    public Element waitForAny(String[] locators, Duration timeout) {\n        Element found = pollFor(timeout.toMillis(), options.getRetryInterval(), () -> {\n            for (String locator : locators) {\n                if (exists(locator)) {\n                    return BaseElement.existing(this, locator);\n                }\n            }\n            return null;\n        });\n        if (found == null) {\n            throw new DriverException(\"timeout waiting for any element: \" + String.join(\", \", locators));\n        }\n        return found;\n    }\n\n    /**\n     * Wait for an element to contain specific text.\n     */\n    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);","sourceCodeStart":3306,"sourceCodeEnd":3342,"githubUrl":"https://github.com/karatelabs/karate/blob/a22eb90246d958d15a47bf436693d0121ad2812d/karate-core/src/main/java/io/karatelabs/driver/cdp/CdpDriver.java#L3306-L3342","documentation":"driver.waitForAny(locators) polls until at least one of the supplied locators exists, then returns an Element for the first match. If none of the locators match within the timeout, it throws with the comma-joined locator list.","triggerScenarios":"driver.waitForAny(String... locators) where every provided selector fails to match during the poll window — e.g. waiting for either a success or error banner but neither rendered because the triggering action failed.","commonSituations":"Race where the preceding action silently failed so neither expected outcome appeared; all candidate selectors stale after a UI redesign; page navigated away so none of the candidates exist; timeout shorter than the app's response time.","solutions":["Check the page state after the triggering action; add asserts/logs to confirm it succeeded before waiting for outcomes.","Update the locator list to cover the actual possible outcomes (inspect the DOM in both branches).","Increase the timeout to accommodate the slowest realistic outcome.","Catch DriverException and capture the page source/driver state to diagnose which branch never rendered."],"exampleFix":"// before\nElement e = driver.waitForAny('.toast-success', '.toast-error'); // neither appears\n// after\ndriver.click('#save');\nElement e = driver.waitForAny('.toast-success', '.toast-error', '.form-error-inline', Duration.ofSeconds(15));","handlingStrategy":"try-catch","validationCode":"// assert the triggering action produced some visible outcome first\nif (driver.exists(\".app-crash-screen\")) throw new AssertionError(\"app crashed before waitAny\");","typeGuard":null,"tryCatchPattern":"try {\n    Element e = driver.waitForAny(\".toast-success\", \".toast-error\");\n} catch (Exception e) {\n    if (e.getMessage().startsWith(\"timeout waiting for any element\")) {\n        throw new AssertionError(\"none of the outcomes appeared: \" + e.getMessage(), e);\n    } else throw e;\n}","preventionTips":["Enumerate all possible UI outcomes in the locator list.","Verify the preceding action succeeded before waiting for its results.","Include a neutral fallback locator (e.g. unchanged-state marker).","Size the timeout for the slowest branch."],"tags":["driver","wait","timeout","multiple-locators"],"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"}