grafana/k6 · error

querying selector %q, wrong type %T

Error message

querying selector %q, wrong type %T

What it means

ElementHandle.Query() type-asserted the evaluation result to a JSHandleAPI and got a different Go type (element_handle.go:1233). This is an internal invariant break — the protocol returned an unexpected result shape — not a user-input error. The %T in the message tells maintainers which type leaked through.

Source

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

	querySelector := `
		(node, injected, selector, strict) => {
			return injected.querySelector(selector, strict, node || document);
		}
	`
	opts := evalOptions{
		forceCallable: true,
		returnByValue: false,
	}
	result, err := h.evalWithScript(h.ctx, opts, querySelector, parsedSelector, strict)
	if err != nil {
		return nil, fmt.Errorf("querying selector %q: %w", selector, err)
	}
	if result == nil {
		return nil, nil //nolint:nilnil
	}
	handle, ok := result.(JSHandleAPI)
	if !ok {
		return nil, fmt.Errorf("querying selector %q, wrong type %T", selector, result)
	}
	element := handle.AsElement()
	if element == nil {
		defer func() {
			if err := handle.Dispose(); err != nil {
				err = fmt.Errorf("disposing element handle: %w", err)
				rerr = errors.Join(err, rerr)
			}
		}()
		return nil, fmt.Errorf("querying selector %q", selector)
	}

	return element, nil
}

// QueryAll queries element subtree for matching elements.
// If no element matches the selector, the return value resolves to "null".
func (h *ElementHandle) QueryAll(selector string) ([]*ElementHandle, error) {

View on GitHub (pinned to 93accf6570)

Solutions

  1. Upgrade k6 to the latest release (the browser module is under active fix)
  2. Pin/try a different chromium version to rule out a protocol deviation
  3. Report to k6 with the minimal script, the %T value from the message, and browser versions
  4. Work around with page.$()/locator queries on the page instead of element.$()
Defensive patterns

Strategy: try-catch

Try / catch

try { const child = await el.$(sel); }
catch (e) { if (/wrong type/.test(e.message)) { /* internal: report + fall back to page.$ */ return await page.$(sel); } throw e; }

Prevention

When it happens

Trigger: A chromium/CDP version returning an unexpected Runtime.callFunctionOn result; a k6 browser-module bug in the evaluation-to-handle plumbing; exotic targets (embedded chromium forks) deviating from the protocol.

Common situations: Rare. Appears after upgrading chromium or running against non-standard browsers/WebViews; not reproducible with normal selectors on stock chrome.

Related errors


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