grafana/k6 · error

only one of the selectors can capture using * modifier

Error message

only one of the selectors can capture using * modifier

What it means

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.

Solutions

  1. Keep exactly one '*' — on the part whose element you actually want returned — and remove the rest
  2. If you need elements from different stages, run separate chained queries and keep both results in script code
  3. Use $$ on the final chain to enumerate matches instead of adding a second capture

Example fix

// before
const el = await page.$('*css=div.item >> *css=span.label >> text=Buy');

// after
const item = await page.$('*css=div.item >> css=span.label >> text=Buy');
const label = await item.$('css=span.label');
Defensive patterns

Strategy: validation

Validate before calling

function validateChain(sel) {
  const captures = sel.split('>>').filter(p => p.trim().startsWith('*'));
  if (captures.length > 1) {
    throw new Error('only one capture (*) allowed in: ' + sel);
  }
  return sel;
}
await page.$(validateChain(sel));

Try / catch

try {
  await page.$(sel);
} catch (e) {
  if (/only one of the selectors can capture/.test(e.message)) {
    // strip all but the first '*' and retry
    let i = 0;
    const fixed = sel.split('>>')
      .map(p => (p.trim().startsWith('*') && i++ > 0 ? p.trim().slice(1) : p))
      .join(' >> ');
    return page.$(fixed);
  }
  throw e;
}

Prevention

When it happens

Trigger: 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.

Common situations: 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.

Related errors


AI-assisted analysis of grafana/k6@01ffac6f24 (2026-08-18). Data as JSON: /api/errors/090f7254db4489fb. Report an issue: GitHub.

Appendix: source

Thrown at internal/js/modules/k6/browser/common/selectors.go:67

func NewSelector(selector string) (*Selector, error) {
	if selector == "" {
		return nil, errors.New("provided selector is empty")
	}

	s := Selector{
		Selector: selector,
		Parts:    make([]*SelectorPart, 0, 1),
		Capture:  nil,
	}
	err := s.parse()
	return &s, err
}

func (s *Selector) appendPart(p *SelectorPart, capture bool) error {
	s.Parts = append(s.Parts, p)
	if capture {
		if s.Capture != nil {
			return errors.New("only one of the selectors can capture using * modifier")
		}
		s.Capture = new(int)
		*s.Capture = (len(s.Parts) - 1)
	}
	return nil
}

// parse splits the selector into parts, separated by `>>`, and identifies
// the query engine for each part.
//
//nolint:cyclop,funlen
func (s *Selector) parse() error {
	parsePart := func(part string) (*SelectorPart, bool) {
		part = strings.TrimSpace(part)
		if part == "" {
			return nil, false
		}

View on GitHub (pinned to 01ffac6f24)