grafana/k6 · error

provided selector is empty

Error message

provided selector is empty

What it means

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.

Solutions

  1. Validate the selector is a non-empty string before calling the API: if (!sel) throw new Error('selector is required: ' + context)
  2. Fix the data source (CSV row, env var, config key) that supplies the empty value
  3. Provide a sensible default selector when the dynamic value is missing
  4. Fail fast in test setup with a clear message naming the missing field

Example fix

// before
const sel = row.selector; // '' for some rows
await page.click(sel);

// after
if (!row.selector) throw new Error(`missing selector for row ${row.id}`);
await page.click(row.selector);
Defensive patterns

Strategy: validation

Validate before calling

function requireSelector(sel, ctx = '') {
  if (typeof sel !== 'string' || sel.trim() === '') {
    throw new Error(`empty selector${ctx ? ' for ' + ctx : ''}`);
  }
  return sel;
}
await page.click(requireSelector(row.selector, `row ${row.id}`));

Type guard

const isNonEmptySelector = s => typeof s === 'string' && s.length > 0;

Try / catch

try {
  await page.$(sel);
} catch (e) {
  if (/provided selector is empty/.test(e.message)) {
    throw new Error(`selector source produced '': ${JSON.stringify(source)}`);
  }
  throw e;
}

Prevention

When it happens

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

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

Related errors


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

Appendix: source

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

type SelectorPart struct {
	Name string `json:"name"`
	Body string `json:"body"`
}

type Selector struct {
	Selector string          `json:"selector"`
	Parts    []*SelectorPart `json:"parts"`

	// By default chained queries resolve to elements matched by the last selector,
	// but a selector can be prefixed with `*` to capture elements resolved by
	// an intermediate selector.
	Capture *int `json:"capture"`
}

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)

View on GitHub (pinned to 01ffac6f24)