{"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/93accf6570dcd306ca5e99cc44c393ee3797761b/internal/js/modules/k6/browser/common/js/injected_script.js#L1029-L1065","documentation":"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.","triggerScenarios":"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.","commonSituations":"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.","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"],"exampleFix":"// before\nconst locator = options.selector; // undefined because options lacks 'selector'\ndoc.find(locator); // GoError: cannot use a '<nil>' as a selector\n\n// after\nconst locator = options.selector;\nif (typeof locator === 'string') {\n  doc.find(locator);\n}","handlingStrategy":"type-guard","validationCode":"const isSelectorArg = (a) =>\n  typeof a === 'string' ||\n  (a !== null && typeof a === 'object' && typeof a.find === 'function' && typeof a.add === 'function');\n\nfunction safeFind(sel, arg) {\n  if (!isSelectorArg(arg)) throw new Error(`invalid selector: ${typeof arg}`);\n  return sel.find(arg);\n}","typeGuard":"function isSelector(a) {\n  if (typeof a === 'string') return true; // CSS selector\n  if (a && typeof a === 'object' && typeof a.find === 'function') return true; // Selection\n  if (a && typeof a === 'object' && a.nodeType === 1) return true; // Element (ok for add/find/closest/has)\n  return false;\n}","tryCatchPattern":"try {\n  doc.find(arg);\n} catch (e) {\n  if (/cannot use a '.*' as a selector/.test(String(e.message))) {\n    // bad dynamic input: log and fall back to a default selector\n    doc.find('.fallback');\n  } else {\n    throw e;\n  }\n}","preventionTips":["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"],"tags":["html","selector","goquery","parsehtml","type-error","javascript"],"backgroundTag":null,"analyzedSha":"93accf6570dcd306ca5e99cc44c393ee3797761b","analyzedAt":"2026-08-15T21:23:27.118Z","schemaVersion":2},"datasetVersion":"2026-08-15T22:17:37.221Z"}