{"record":{"id":"dddd2884916807e4","repo":"grafana/k6","slug":"error-while-parsing-selector-selector-e-me","errorCode":null,"errorMessage":"Error while parsing selector `${selector}`: ${e.message}","messagePattern":"Error while parsing selector `(.+?)`: (.+?)","errorType":"exception","errorClass":"InvalidSelectorError","httpStatus":null,"severity":"error","filePath":"internal/js/modules/k6/browser/common/js/injected_script.js","lineNumber":1047,"sourceCode":"          syntaxError(\"parsing regular expression\");\n      } else if (inClass && next() === \"]\") {\n        inClass = false;\n      } else if (!inClass && next() === \"[\") {\n        inClass = true;\n      } else if (!inClass && next() === \"/\") {\n        break;\n      }\n      source += eat1();\n    }\n    if (eat1() !== \"/\")\n      syntaxError(\"parsing regular expression\");\n    let flags = \"\";\n    while (!EOL && next().match(/[dgimsuy]/))\n      flags += eat1();\n    try {\n      return new RegExp(source, flags);\n    } catch (e) {\n      throw new InvalidSelectorError(`Error while parsing selector \\`${selector}\\`: ${e.message}`);\n    }\n  }\n  function readAttributeToken() {\n    let token = \"\";\n    skipSpaces();\n    if (next() === `'` || next() === `\"`)\n      token = readQuotedString(next()).slice(1, -1);\n    else\n      token = readIdentifier();\n    if (!token)\n      syntaxError(\"parsing property path\");\n    return token;\n  }\n  function readOperator() {\n    skipSpaces();\n    let op = \"\";\n    if (!EOL)\n      op += eat1();","sourceCodeStart":1029,"sourceCodeEnd":1065,"githubUrl":"https://github.com/grafana/k6/blob/3fcf5388d78cb382f0c0d42ec556a06bfd1b8dee/internal/js/modules/k6/browser/common/js/injected_script.js#L1029-L1065","documentation":"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.","triggerScenarios":"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.","commonSituations":"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.","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."],"exampleFix":"// before\nawait page.focus('#search');\n\n// after\nawait page.waitForSelector('#search', { state: 'attached', timeout: 10000 });\nawait page.focus('#search');","handlingStrategy":"try-catch","validationCode":"const ok = await page.waitForSelector('#search', { state: 'attached', timeout: 5000 });\nif (!ok) throw new Error('search box not present');","typeGuard":null,"tryCatchPattern":"try {\n  await page.focus('#search', { timeout: 10000 });\n} catch (e) {\n  if (String(e).startsWith('focusing')) {\n    // element missing or frame churn; wait and retry once\n    await page.waitForLoadState('domcontentloaded');\n    await page.focus('#search', { timeout: 10000 });\n  } else throw e;\n}","preventionTips":["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."],"tags":["k6","browser","frame","focus","selector","timeout"],"backgroundTag":"element-not-found-timeout","analyzedSha":"3fcf5388d78cb382f0c0d42ec556a06bfd1b8dee","analyzedAt":"2026-08-18T03:05:52.393Z","contentChangedAt":"2026-08-18T03:05:52.393Z","schemaVersion":2},"datasetVersion":"2026-09-23T08:17:48.524Z"}