grafana/k6 · error

selecting option on %q: %w

Error message

selecting option on %q: %w

What it means

Thrown by Locator.SelectOption() when the wrapped frame.selectOption call fails. Strict mode is forced on, so exactly one element must match. Typical underlying failures: strict mode violation (multiple matches), timeout waiting for the <select> to be actionable, or the resolved element not being a <select> element at all.

Source

Thrown at internal/js/modules/k6/browser/common/locator.go:530

	opts.Strict = true
	v, err := l.frame.inputValue(l.selector, opts)
	if err != nil {
		return "", fmt.Errorf("getting input value of %q: %w", l.selector, err)
	}

	return v, nil
}

// SelectOption filters option values of the first element that matches
// the locator's selector (with strict mode on), selects the options,
// and returns the filtered options.
func (l *Locator) SelectOption(values []any, opts *FrameSelectOptionOptions) ([]string, error) {
	l.log.Debugf("Locator:SelectOption", "fid:%s furl:%q sel:%q opts:%+v", l.frame.ID(), l.frame.URL(), l.selector, opts)

	opts.Strict = true
	v, err := l.frame.selectOption(l.selector, values, opts)
	if err != nil {
		return nil, fmt.Errorf("selecting option on %q: %w", l.selector, err)
	}

	applySlowMo(l.ctx)

	return v, nil
}

// Press the given key on the element found that matches the locator's
// selector with strict mode on.
func (l *Locator) Press(key string, opts *FramePressOptions) error {
	l.log.Debugf(
		"Locator:Press", "fid:%s furl:%q sel:%q key:%q opts:%+v",
		l.frame.ID(), l.frame.URL(), l.selector, key, opts,
	)

	opts.Strict = true
	if err := l.frame.press(l.selector, key, opts); err != nil {
		return fmt.Errorf("pressing %q on %q: %w", key, l.selector, err)

View on GitHub (pinned to 93accf6570)

Solutions

  1. Use a selector that matches exactly one <select> (add id or data-testid)
  2. await locator.waitFor() first so the dropdown is rendered
  3. Verify the target is a real <select> element, not a custom div-based dropdown
  4. Match the option exactly by value or label (inspect the option elements first)

Example fix

// before
await page.locator('select').selectOption('DE'); // multiple selects -> strict mode violation

// after
await page.locator('select#country').waitFor();
await page.locator('select#country').selectOption({ value: 'DE' });
Defensive patterns

Strategy: try-catch

Validate before calling

const selects = await page.$$('select#country');
if (selects.length !== 1) throw new Error(`expected one select, found ${selects.length}`);
await page.locator('select#country').selectOption({ value: 'DE' });

Type guard

async function isSelectElement(page, sel) {
  const el = await page.$(sel);
  return !!el && (await el.getProperty('tagName')).jsonValue() === 'SELECT';
}

Try / catch

try {
  await page.locator(sel).selectOption(values);
} catch (e) {
  if (/strict mode violation/i.test(e.message)) { /* narrow selector */ }
  else if (/not a select/i.test(e.message)) { /* custom dropdown: click options instead */ }
  else throw e;
}

Prevention

When it happens

Trigger: locator.selectOption(...) where the selector matches 0 or >1 elements, the element is not a <select>, or the select is disabled/hidden past the timeout so it never becomes actionable.

Common situations: Selecting from a dropdown rendered asynchronously (React/Vue select); selectors like 'select' matching several dropdowns on a form; passing values whose options are not present so nothing can be selected; custom dropdown widgets that use <div>/<ul> instead of <select>.

Related errors


AI-assisted analysis of grafana/k6@93accf6570 (2026-08-15). Data as JSON: /api/errors/e6c459ecb5351312. Report an issue: GitHub.