{"record":{"id":"0da7ce95bff22b09","repo":"karatelabs/karate","slug":"bad-wildcard-locator-locator","errorCode":null,"errorMessage":"bad wildcard locator: {locator}","messagePattern":"bad wildcard locator: (.+?)","errorType":"exception","errorClass":"DriverException","httpStatus":null,"severity":"error","filePath":"karate-core/src/main/java/io/karatelabs/driver/Locators.java","lineNumber":142,"sourceCode":"        return false;\n    }\n\n    // ========== Wildcard Locator Expansion ==========\n\n    /**\n     * Expand wildcard locator to JavaScript resolver call.\n     * Uses browser-side JS that matches the same logic as locator generation.\n     * <ul>\n     *   <li>{tag}text     → window.__kjs.resolve('tag', 'text', 1, false)</li>\n     *   <li>{^tag}text    → window.__kjs.resolve('tag', 'text', 1, true)</li>\n     *   <li>{tag:2}text   → window.__kjs.resolve('tag', 'text', 2, false)</li>\n     *   <li>{:2}text      → window.__kjs.resolve('*', 'text', 2, false)</li>\n     * </ul>\n     */\n    public static String expandWildcard(String locator) {\n        Matcher m = WILDCARD_PATTERN.matcher(locator);\n        if (!m.matches()) {\n            throw new DriverException(\"bad wildcard locator: \" + locator);\n        }\n\n        boolean contains = m.group(1) != null;  // ^ prefix\n        String tagPart = m.group(2);\n        String indexPart = m.group(3);\n        String text = m.group(4);\n\n        String tag = (tagPart == null || tagPart.isEmpty()) ? \"*\" : tagPart;\n        int index = (indexPart != null) ? Integer.parseInt(indexPart) : 1;\n\n        // Build JS resolver call\n        String escapedTag = escapeForJs(tag);\n        String escapedText = escapeForJs(text);\n        return \"window.__kjs.resolve(\\\"\" + escapedTag + \"\\\", \\\"\" + escapedText + \"\\\", \" + index + \", \" + contains + \")\";\n    }\n\n    // ========== XPath Selector ==========\n","sourceCodeStart":124,"sourceCodeEnd":160,"githubUrl":"https://github.com/karatelabs/karate/blob/a22eb90246d958d15a47bf436693d0121ad2812d/karate-core/src/main/java/io/karatelabs/driver/Locators.java#L124-L160","documentation":"expandWildcard converts Karate wildcard locators like {div}, {:2}text, {^}text into JS resolver expressions. When the locator starts like a wildcard but does not match the wildcard grammar (WILDCARD_PATTERN), it cannot be expanded and a DriverException is thrown.","triggerScenarios":"Passing a malformed wildcard such as '{div' (unclosed), '{:}' (empty parts), or '{foo:bar:baz}' — any '{'-prefixed string that fails the pattern for tag/index/text groups.","commonSituations":"Typos in wildcard syntax, mixing wildcard syntax with css/xpath prefixes, upgrading Karate versions where wildcard grammar was tightened, hand-written wildcards generated from variables.","solutions":["Fix the wildcard syntax to match the supported grammar (e.g. {div}, {div}text, {:2}text, {^}text)","If it is not meant to be a wildcard, remove the leading '{' or use an explicit 'css:'/'xpath:' prefix","Log/print the composed locator string to spot malformed interpolation"],"exampleFix":"// before\ndriver.locate(\"{div'); // malformed\n// after\ndriver.locate(\"{div}Submit\"); // wildcard: div containing text 'Submit'","handlingStrategy":"validation","validationCode":"// only pass '{'-prefixed strings that match known wildcard forms\nif (locator.startsWith(\"{\") && !locator.matches(\"\\{\\^?\\w*(::?\\d+)?\\}.*|\\{\\^?\\w*\\}.*\")) {\n    throw new IllegalArgumentException(\"malformed wildcard locator: \" + locator);\n}","typeGuard":null,"tryCatchPattern":"try {\n    driver.locate(wildcard);\n} catch (DriverException e) {\n    if (e.getMessage().startsWith(\"bad wildcard locator\")) {\n        throw new IllegalArgumentException(\"Use forms like {div}, {div}text, {:2}text, {^}text: \" + wildcard, e);\n    } throw e;\n}","preventionTips":["Stick to documented wildcard forms: {tag}, {tag}text, {:N}text, {^}text","Use explicit css:/xpath: prefixes when unsure","Test dynamically composed wildcards with a print/log first"],"tags":["driver","locator","wildcard"],"backgroundTag":"invalid-argument-format","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"}