grafana/k6 · error · InvalidSelectorError
Error while parsing selector
Error message
Error while parsing selector `${selector}`: ${e.message} What it means
Frame.Focus() (frame.go:961) wraps failures of the internal focus action with 'focusing %q'. The action resolves the first element matching the selector in the attached state within Timeout and calls ElementHandle.focus (scrollIntoView + DOM focus). Timeouts waiting for the element, strict-mode violations, and evaluation/context errors during the focus call all bubble up inside this message.
Solutions
- Confirm the selector matches: const h = await page.waitForSelector(sel, { timeout: 5000 }); then focus.
- Use a unique, stable selector (data-testid) and enable Strict only when uniqueness is guaranteed.
- Increase the per-call timeout: page.focus(sel, { timeout: 60000 }).
- If a previous step (opening a menu, switching a tab) may have failed, assert on it before focusing.
Example fix
// before
await page.focus('#search');
// after
await page.waitForSelector('#search', { state: 'attached', timeout: 10000 });
await page.focus('#search'); Defensive patterns
Strategy: try-catch
Validate before calling
const ok = await page.waitForSelector('#search', { state: 'attached', timeout: 5000 });
if (!ok) throw new Error('search box not present'); Try / catch
try {
await page.focus('#search', { timeout: 10000 });
} catch (e) {
if (String(e).startsWith('focusing')) {
// element missing or frame churn; wait and retry once
await page.waitForLoadState('domcontentloaded');
await page.focus('#search', { timeout: 10000 });
} else throw e;
} Prevention
- Verify prerequisite UI steps (menu open, tab switch) succeeded before focusing their children.
- Use unique selectors to avoid strict-mode failures.
- Re-acquire frames after any step that can navigate.
When it happens
Trigger: No element matches the selector before opts.Timeout expires; strict mode on and multiple matches; element exists inside a frame that navigates or detaches during the action; the element handle's focus evaluation fails because the execution context was destroyed.
Common situations: Focusing controls that are lazily rendered (modals, dropdowns opened by prior steps that failed silently); typos or stale selectors after UI redesigns; iframes whose content reloads while the script focuses inside them.
Related errors
- an error occurred communicating with k6 Cloud
- " " does not support " " matcher
- Error while parsing selector
- Error while parsing selector
- Malformed attribute selector
AI-assisted analysis of grafana/k6@3fcf5388d7 (2026-08-18).
Data as JSON: /api/errors/dddd2884916807e4.
Report an issue: GitHub.
Appendix: 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 3fcf5388d7)