grafana/k6 · error

strict mode violation, multiple elements returned for select

Error message

strict mode violation, multiple elements returned for selector query

What it means

Mapped from the injected-script error 'error:strictmodeviolation' by errorFromDOMError (element_handle.go:1896-1916). When an element query or action runs with the strict:true option, the selector must resolve to exactly one element; if it matches two or more nodes, the operation is aborted with this error instead of silently using the first match.

Source

Thrown at internal/js/modules/k6/browser/common/element_handle.go:1915

		"error:notelement":             "node is not an element",
		"error:nothtmlelement":         "not an HTMLElement",
		"error:notfillableelement":     "element is not an <input>, <textarea> or [contenteditable] element",
		"error:notfillableinputtype":   "input of this type cannot be filled",
		"error:notfillablenumberinput": "cannot type text into input[type=number]",
		"error:notvaliddate":           "malformed value",
		"error:notinput":               "node is not an HTMLInputElement",
		"error:notfile":                "node is not an input[type=file] element",
		"error:hasnovalue":             "node is not an HTMLInputElement or HTMLTextAreaElement or HTMLSelectElement",
		"error:notselect":              "element is not a <select> element",
		"error:notcheckbox":            "not a checkbox or radio button",
		"error:notmultiplefileinput":   "non-multiple file input can only accept single file",
		"error:strictmodeviolation":    "strict mode violation, multiple elements returned for selector query",
		"error:notqueryablenode":       "node is not queryable",
		"error:nthnocapture":           "can't query n-th element in a chained selector with capture",
		"error:intercept":              "another element is intercepting with pointer action",
	}
	if err, ok := errs[serr]; ok {
		return errors.New(err)
	}

	return errors.New(serr)
}

View on GitHub (pinned to 01ffac6f24)

Solutions

  1. Make the selector specific: add an id, data-testid, or scope it with a chained selector to a container
  2. Select one match explicitly with a chained nth= part or by indexing $$(selector) results
  3. Drop strict:true to fall back to first-match behavior (k6 default)
  4. If ambiguity is itself the bug, use $$(selector) and assert on the expected count

Example fix

// before
await page.click('text=Submit', { strict: true }); // matches 2 buttons

// after
await page.click('#order-form >> text=Submit', { strict: true });
Defensive patterns

Strategy: validation

Validate before calling

// Count matches before running a strict operation
const matches = await page.$$('text=Submit');
if (matches.length !== 1) {
  throw new Error(`expected 1 'Submit', found ${matches.length}`);
}
await page.click('text=Submit', { strict: true });

Try / catch

try {
  await page.click(sel, { strict: true });
} catch (e) {
  if (/strict mode violation/.test(e.message)) {
    const all = await page.$$(sel);
    await all[0].click(); // deliberate first-match fallback
  } else throw e;
}

Prevention

When it happens

Trigger: page.click('#btn', {strict: true}), frame.waitForSelector(sel, {strict: true}), or elementHandle.$(sel, {strict: true}) where the selector matches 2+ nodes. Strict is opt-in in k6 browser (options default Strict:false in frame_options.go), so this only fires when explicitly enabled or when an API path enforces it.

Common situations: Broad selectors like text=Submit or css=button matching repeated components (table rows, cards, nav + footer links); developers coming from Playwright where strict is the default and enabling it here exposes ambiguous selectors.

Related errors


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