{"record":{"id":"65f259a3aba664eb","repo":"karatelabs/karate","slug":"select-failed-no-option-matching-text-for-locator-cdpdriver","errorCode":null,"errorMessage":"select failed, no option matching: '{text}' for: {locator}","messagePattern":"select failed, no option matching: '(.+?)' for: (.+?)","errorType":"exception","errorClass":"DriverException","httpStatus":null,"severity":"error","filePath":"karate-core/src/main/java/io/karatelabs/driver/cdp/CdpDriver.java","lineNumber":2983,"sourceCode":"    }\n\n    /**\n     * Set the value of an input element.\n     */\n    public Element value(String locator, String value) {\n        logger.debug(\"value: {} <- {}\", locator, value);\n        elementAction(locator, Locators.inputJs(locator, value));\n        return BaseElement.existing(this, locator);\n    }\n\n    /**\n     * Select an option from a dropdown by text or value.\n     */\n    public Element select(String locator, String text) {\n        logger.debug(\"select: {} <- {}\", locator, text);\n        Object matched = elementAction(locator, Locators.optionSelector(locator, text));\n        if (!Boolean.TRUE.equals(matched)) {\n            throw new DriverException(\"select failed, no option matching: '\" + text + \"' for: \" + locator);\n        }\n        return BaseElement.existing(this, locator);\n    }\n\n    /**\n     * Select an option from a dropdown by index.\n     */\n    public Element select(String locator, int index) {\n        logger.debug(\"select: {} <- index {}\", locator, index);\n        retryIfNeeded(locator);\n        String js = Locators.wrapInFunctionInvoke(\n                \"var e = \" + Locators.selector(locator) + \";\" +\n                        \" e.options[\" + index + \"].selected = true;\" +\n                        \" \" + Locators.commitFieldEventsJs(\"e\"));\n        script(js);\n        return BaseElement.existing(this, locator);\n    }\n","sourceCodeStart":2965,"sourceCodeEnd":3001,"githubUrl":"https://github.com/karatelabs/karate/blob/a22eb90246d958d15a47bf436693d0121ad2812d/karate-core/src/main/java/io/karatelabs/driver/cdp/CdpDriver.java#L2965-L3001","documentation":"Karate's select() locates an <option> inside a dropdown by matching the given text (or value) via Locators.optionSelector and executes the option-selection action. If the action does not report success, no option matching the requested text exists in the select element.","triggerScenarios":"driver.select(locator, text) (or * select step) where the option text/value does not exist in the dropdown, the locator points to a non-<select> element, or option markup uses whitespace/case that prevents the exact match.","commonSituations":"Dropdown options are loaded asynchronously after page load and the select ran too early; test data expects an option the app no longer offers; matching on visible label but the option's text differs (extra whitespace, HTML entities); custom JS dropdowns that are not native <select> elements.","solutions":["Dump the dropdown's options at runtime and select an exact existing text/value.","Wait for the options to load before selecting (e.g. driver.waitFor(locator + ' option')).","Normalize case/whitespace in the text argument, or match by value attribute instead of label.","Confirm the locator is a native <select>; for custom dropdown components click the component and option elements instead."],"exampleFix":"// before\ndriver.select('#country', ' USA '); // mismatched whitespace\n// after\ndriver.waitFor('#country option');\ndriver.select('#country', 'USA');","handlingStrategy":"validation","validationCode":"// check the option exists before selecting\nString script = \"return Array.from(document.querySelectorAll('\" + selectLocator + \" option')).some(o => o.text.trim() === '\" + text + \"')\";\nBoolean ok = driver.script(script, null, Boolean.class);\nif (!Boolean.TRUE.equals(ok)) throw new IllegalStateException(\"option missing: \" + text);","typeGuard":null,"tryCatchPattern":"try {\n    driver.select(locator, text);\n} catch (Exception e) {\n    if (e.getMessage().contains(\"select failed, no option matching\")) {\n        driver.waitFor(locator + \" option\"); // options loaded late\n        driver.select(locator, text.trim());\n    } else throw e;\n}","preventionTips":["waitFor option elements before selecting.","Trim/normalize text and match on value when labels vary.","Confirm the target is a native <select>, not a JS widget.","Keep expected option values in sync with app copy."],"tags":["driver","select","dropdown","element-not-found"],"backgroundTag":"entity-not-found","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"}