{"record":{"id":"090f7254db4489fb","repo":"grafana/k6","slug":"only-one-of-the-selectors-can-capture-using-modi","errorCode":null,"errorMessage":"only one of the selectors can capture using * modifier","messagePattern":"only one of the selectors can capture using \\* modifier","errorType":"validation","errorClass":null,"httpStatus":null,"severity":"error","filePath":"internal/js/modules/k6/browser/common/selectors.go","lineNumber":67,"sourceCode":"func 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)\n\t\t*s.Capture = (len(s.Parts) - 1)\n\t}\n\treturn nil\n}\n\n// parse splits the selector into parts, separated by `>>`, and identifies\n// the query engine for each part.\n//\n//nolint:cyclop,funlen\nfunc (s *Selector) parse() error {\n\tparsePart := func(part string) (*SelectorPart, bool) {\n\t\tpart = strings.TrimSpace(part)\n\t\tif part == \"\" {\n\t\t\treturn nil, false\n\t\t}\n","sourceCodeStart":49,"sourceCodeEnd":85,"githubUrl":"https://github.com/grafana/k6/blob/01ffac6f245854c1b8adc6a69857c76a15f90022/internal/js/modules/k6/browser/common/selectors.go#L49-L85","documentation":"Selector.appendPart (selectors.go:59-72) implements chained selectors (parts joined with '>>'). Prefixing one part with '*' marks it as the capture point — the element that intermediate part resolves to is returned instead of the last part (selectors.go:44-46). The parser records at most one capture index (s.Capture), so a second '*'-prefixed part in the same chain is rejected.","triggerScenarios":"A selector string with two or more '*'-prefixed parts, e.g. page.$('*css=div.item >> *css=span.label >> text=Buy'). The first '*' sets s.Capture; the second hits the 's.Capture != nil' branch and errors.","commonSituations":"Building chains programmatically and marking every part '*'; developers porting Playwright locator chains assuming multiple captures can be composed; misunderstanding '*' as a wildcard rather than a capture marker.","solutions":["Keep exactly one '*' — on the part whose element you actually want returned — and remove the rest","If you need elements from different stages, run separate chained queries and keep both results in script code","Use $$ on the final chain to enumerate matches instead of adding a second capture"],"exampleFix":"// before\nconst el = await page.$('*css=div.item >> *css=span.label >> text=Buy');\n\n// after\nconst item = await page.$('*css=div.item >> css=span.label >> text=Buy');\nconst label = await item.$('css=span.label');","handlingStrategy":"validation","validationCode":"function validateChain(sel) {\n  const captures = sel.split('>>').filter(p => p.trim().startsWith('*'));\n  if (captures.length > 1) {\n    throw new Error('only one capture (*) allowed in: ' + sel);\n  }\n  return sel;\n}\nawait page.$(validateChain(sel));","typeGuard":null,"tryCatchPattern":"try {\n  await page.$(sel);\n} catch (e) {\n  if (/only one of the selectors can capture/.test(e.message)) {\n    // strip all but the first '*' and retry\n    let i = 0;\n    const fixed = sel.split('>>')\n      .map(p => (p.trim().startsWith('*') && i++ > 0 ? p.trim().slice(1) : p))\n      .join(' >> ');\n    return page.$(fixed);\n  }\n  throw e;\n}","preventionTips":["Remember '*' means 'capture this stage', not wildcard","Keep exactly one capture per chain — on the stage you want returned","Build chains through a helper that counts '*' prefixes"],"tags":["browser","selector","chained-selector","parse-error"],"backgroundTag":"invalid-selector-syntax","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"}