grafana/k6 · error

query: %w

Error message

query: %w

What it means

Thrown by Frame.runActionOnSelector when f.Query(selector, strict) itself errors before any element action runs. Query failure means the selector could not be evaluated by the browser (invalid selector syntax) or strict mode rejected it (multiple/zero matches surfaced as error rather than nil). The %w carries the precise query error.

Source

Thrown at internal/js/modules/k6/browser/common/frame.go:2360

	Eval(apiCtx context.Context, js string, args ...any) (any, error)

	// EvalHandle evaluates the provided JavaScript within this execution
	// context and returns a JSHandle.
	EvalHandle(apiCtx context.Context, js string, args ...any) (JSHandleAPI, error)

	// Frame returns the frame that this execution context belongs to.
	Frame() *Frame

	// id returns the CDP runtime ID of this execution context.
	ID() runtime.ExecutionContextID
}

func (f *Frame) runActionOnSelector(
	ctx context.Context, selector string, strict bool, fn elementHandleActionFunc, nullResponder func() bool,
) (bool, error) {
	handle, err := f.Query(selector, strict)
	if err != nil {
		return false, fmt.Errorf("query: %w", err)
	}
	if handle == nil {
		f.log.Debugf("Frame:runActionOnSelector:nilHandler", "fid:%s furl:%q selector:%s", f.ID(), f.URL(), selector)
		return nullResponder(), err
	}

	v, err := fn(ctx, handle)
	if err != nil {
		return false, fmt.Errorf("calling function: %w", err)
	}

	bv, ok := v.(bool)
	if !ok {
		return false, fmt.Errorf("unexpected type %T", v)
	}

	return bv, nil
}

View on GitHub (pinned to 93accf6570)

Solutions

  1. Paste the selector into DevTools document.querySelectorAll(...) to confirm it parses and matches exactly one node
  2. Make the selector unique (add :nth-of-type, an id, or a data-test attribute)
  3. Pass strict: false where clicking any of several matches is acceptable

Example fix

// before
await page.click('div > button'); // strict: 3 matches -> query error

// after
await page.click('div.card--primary > button');
Defensive patterns

Strategy: validation

Validate before calling

const n = await page.evaluate((s) => document.querySelectorAll(s).length, sel);
if (n !== 1) throw new Error(`${sel} matches ${n} nodes; expected exactly 1`);

Type guard

function isQueryError(e) {
  return e instanceof Error && /^query:/.test(e.message);
}

Prevention

When it happens

Trigger: page.click('a[href=') with broken syntax; page.focus('#x') matching 2+ nodes with strict mode on; querying in a detached frame. Note this differs from a timeout: it fails fast on the query step, not after waiting.

Common situations: Dynamically built selectors with unescaped quotes/brackets; generic selectors like 'div > button' used on pages with repeated component instances; operating on a frame removed during SPA rerenders.

Related errors


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