{"record":{"id":"d08ee3155c22b8a7","repo":"grafana/k6","slug":"provided-selector-is-empty","errorCode":null,"errorMessage":"provided selector is empty","messagePattern":"provided selector is empty","errorType":"validation","errorClass":null,"httpStatus":null,"severity":"error","filePath":"internal/js/modules/k6/browser/common/selectors.go","lineNumber":51,"sourceCode":"\ntype SelectorPart struct {\n\tName string `json:\"name\"`\n\tBody string `json:\"body\"`\n}\n\ntype Selector struct {\n\tSelector string          `json:\"selector\"`\n\tParts    []*SelectorPart `json:\"parts\"`\n\n\t// By default chained queries resolve to elements matched by the last selector,\n\t// but a selector can be prefixed with `*` to capture elements resolved by\n\t// an intermediate selector.\n\tCapture *int `json:\"capture\"`\n}\n\nfunc NewSelector(selector string) (*Selector, error) {\n\tif selector == \"\" {\n\t\treturn nil, errors.New(\"provided selector is empty\")\n\t}\n\n\ts := Selector{\n\t\tSelector: selector,\n\t\tParts:    make([]*SelectorPart, 0, 1),\n\t\tCapture:  nil,\n\t}\n\terr := s.parse()\n\treturn &s, err\n}\n\nfunc (s *Selector) appendPart(p *SelectorPart, capture bool) error {\n\ts.Parts = append(s.Parts, p)\n\tif capture {\n\t\tif s.Capture != nil {\n\t\t\treturn errors.New(\"only one of the selectors can capture using * modifier\")\n\t\t}\n\t\ts.Capture = new(int)","sourceCodeStart":33,"sourceCodeEnd":69,"githubUrl":"https://github.com/grafana/k6/blob/01ffac6f245854c1b8adc6a69857c76a15f90022/internal/js/modules/k6/browser/common/selectors.go#L33-L69","documentation":"NewSelector (selectors.go:50-52) parses every selector string used by k6 browser element queries and actions. An empty string has no parts to parse, so the parser rejects it immediately with this error before any CDP traffic happens.","triggerScenarios":"page.$(''), frame.click(''), or any query/action API called with '' — typically a selector built by concatenation or template literal where a variable resolved to an empty string.","commonSituations":"Data-driven tests where the selector column is missing for some rows; selectors read from env vars or config that were never set; optional selector fragments concatenated without a default; whitespace-trimmed input becoming empty.","solutions":["Validate the selector is a non-empty string before calling the API: if (!sel) throw new Error('selector is required: ' + context)","Fix the data source (CSV row, env var, config key) that supplies the empty value","Provide a sensible default selector when the dynamic value is missing","Fail fast in test setup with a clear message naming the missing field"],"exampleFix":"// before\nconst sel = row.selector; // '' for some rows\nawait page.click(sel);\n\n// after\nif (!row.selector) throw new Error(`missing selector for row ${row.id}`);\nawait page.click(row.selector);","handlingStrategy":"validation","validationCode":"function requireSelector(sel, ctx = '') {\n  if (typeof sel !== 'string' || sel.trim() === '') {\n    throw new Error(`empty selector${ctx ? ' for ' + ctx : ''}`);\n  }\n  return sel;\n}\nawait page.click(requireSelector(row.selector, `row ${row.id}`));","typeGuard":"const isNonEmptySelector = s => typeof s === 'string' && s.length > 0;","tryCatchPattern":"try {\n  await page.$(sel);\n} catch (e) {\n  if (/provided selector is empty/.test(e.message)) {\n    throw new Error(`selector source produced '': ${JSON.stringify(source)}`);\n  }\n  throw e;\n}","preventionTips":["Validate selectors at data-load time, not at click time","Name the data row/config key in validation errors","Default empty fragments to a known-good selector or fail the setup"],"tags":["browser","selector","validation"],"backgroundTag":"empty-selector","analyzedSha":"01ffac6f245854c1b8adc6a69857c76a15f90022","analyzedAt":"2026-08-18T03:05:52.393Z","contentChangedAt":"2026-08-18T03:05:52.393Z","schemaVersion":2},"datasetVersion":"2026-09-23T08:17:48.524Z"}