grafana/k6 · error · InvalidSelectorError
Error while parsing selector `${selector}`: ${e.message}
Error message
Error while parsing selector `${selector}`: ${e.message} What it means
Thrown as a GoError JavaScript exception by Selection.varargFnCall (js/modules/k6/html/html.go:93) when the selector argument passed to an HTML Selection method is not one of the supported types. The dispatch accepts a Selection, a string, an Element, or a sobek.Value (which is unwrapped and re-dispatched); every other Go type falls through to the default branch and panics via rt.NewGoError. This guards the jQuery-style methods Add, Find, Closest, Has and Not, which forward to goquery's string/Selection/node filter functions.
Source
Thrown at internal/js/modules/k6/browser/common/js/injected_script.js:1047
syntaxError("parsing regular expression");
} else if (inClass && next() === "]") {
inClass = false;
} else if (!inClass && next() === "[") {
inClass = true;
} else if (!inClass && next() === "/") {
break;
}
source += eat1();
}
if (eat1() !== "/")
syntaxError("parsing regular expression");
let flags = "";
while (!EOL && next().match(/[dgimsuy]/))
flags += eat1();
try {
return new RegExp(source, flags);
} catch (e) {
throw new InvalidSelectorError(`Error while parsing selector \`${selector}\`: ${e.message}`);
}
}
function readAttributeToken() {
let token = "";
skipSpaces();
if (next() === `'` || next() === `"`)
token = readQuotedString(next()).slice(1, -1);
else
token = readIdentifier();
if (!token)
syntaxError("parsing property path");
return token;
}
function readOperator() {
skipSpaces();
let op = "";
if (!EOL)
op += eat1();View on GitHub (pinned to 93accf6570)
Solutions
- Pass a CSS selector string: `doc.find('.item')`
- To search within a previous result, pass another Selection: `doc.find(items)`
- To use a node, pass an Element: `doc.find(doc.find('h1').get(0))`
- Check dynamic inputs before use: `if (typeof sel === 'string') doc.find(sel);`
- Wrap the call in try/catch on GoError if the selector is user-supplied at runtime
Example fix
// before
const locator = options.selector; // undefined because options lacks 'selector'
doc.find(locator); // GoError: cannot use a '<nil>' as a selector
// after
const locator = options.selector;
if (typeof locator === 'string') {
doc.find(locator);
} Defensive patterns
Strategy: type-guard
Validate before calling
const isSelectorArg = (a) =>
typeof a === 'string' ||
(a !== null && typeof a === 'object' && typeof a.find === 'function' && typeof a.add === 'function');
function safeFind(sel, arg) {
if (!isSelectorArg(arg)) throw new Error(`invalid selector: ${typeof arg}`);
return sel.find(arg);
} Type guard
function isSelector(a) {
if (typeof a === 'string') return true; // CSS selector
if (a && typeof a === 'object' && typeof a.find === 'function') return true; // Selection
if (a && typeof a === 'object' && a.nodeType === 1) return true; // Element (ok for add/find/closest/has)
return false;
} Try / catch
try {
doc.find(arg);
} catch (e) {
if (/cannot use a '.*' as a selector/.test(String(e.message))) {
// bad dynamic input: log and fall back to a default selector
doc.find('.fallback');
} else {
throw e;
}
} Prevention
- Validate selector inputs from options/__ENV with typeof checks before passing them to find/add/closest/has/not
- Keep selectors as plain strings end-to-end instead of passing around mixed node/selection values
- Remember null/undefined is NOT a valid selector here — default to a real selector string
When it happens
Trigger: Calling `sel.add(arg)`, `sel.find(arg)`, `sel.closest(arg)`, `sel.has(arg)` or `sel.not(arg)` on a Selection returned by parseHTML() where arg is a number, boolean, plain object, array, null or undefined (nil exports also land in the default case and print '<nil>' for %T). Typical case: passing a DOM property or a variable that is undefined, e.g. `doc.find(locator)` where locator came from an options object that lacks the key.
Common situations: Dynamic selectors sourced from __ENV or options that are undefined; confusing k6's Element (returned by sel.get(0), which IS accepted here) with a plain JS object; passing an array of selectors expecting multi-select; refactoring jQuery code that tolerated odd arguments.
Related errors
- Error while parsing selector `${selector}` - cannot use ${op
- Unexpected end of selector while parsing selector `${selecto
- the argument to each() must be a function
- the argument to filter() must be a function, a selector or a
- the argument to is() must be a function, a selector or a sel
AI-assisted analysis of grafana/k6@93accf6570 (2026-08-15).
Data as JSON: /api/errors/dddd2884916807e4.
Report an issue: GitHub.