grafana/k6 · error

getting attribute of element

Error message

getting attribute %q of element: %w

What it means

ElementHandle.getAttribute() wraps the failure of a CDP evaluation that runs element.getAttribute(name) with returnByValue. A missing attribute is NOT an error (the JS API returns null for that); this error means the evaluation itself failed - detached handle, destroyed execution context, timeout - or the attribute name broke the injected script, since the name is interpolated directly into the JavaScript source string.

Solutions

  1. Sanitize the attribute name: allow only /^[A-Za-z_:][-A-Za-z0-9_:.]*$/ before calling getAttribute
  2. Re-acquire the element handle after any navigation or re-render
  3. For exotic names, read via evaluation instead: await el.evaluate((e, n) => e.getAttribute(n), name)
  4. Increase the timeout via options or K6_BROWSER_TIMEOUT if the session is slow

Example fix

// before
const name = getDataAttr(); // "data-q'tag" -> breaks injected JS
const v = await el.getAttribute(name);

// after
const name = getDataAttr();
if (!/^[A-Za-z_:][-A-Za-z0-9_:.]*$/.test(name)) {
  const v = await el.evaluate((e, n) => e.getAttribute(n), name);
} else {
  const v = await el.getAttribute(name);
}
Defensive patterns

Strategy: validation

Validate before calling

// Validate the attribute name (it is interpolated into injected JS) and handle freshness
const NAME = /^[A-Za-z_:][-A-Za-z0-9_:.]*$/;
if (!NAME.test(name)) {
  const v = await el.evaluate((e, n) => e.getAttribute(n), name);
} else {
  const v = await el.getAttribute(name); // null when missing, never an error
}

Try / catch

try {
  const v = await el.getAttribute(name);
} catch (e) {
  if (/Could not find object|Execution context|timed out/i.test(String(e.message))) {
    const fresh = await page.$(sel);
    const v = await fresh.getAttribute(name);
  } else throw e;
}

Prevention

When it happens

Trigger: The attribute name contains a single quote, backslash, or newline, which breaks the generated JS ('element.getAttribute('weird'name')'); the element handle is detached or the execution context was destroyed by navigation; the evaluation exceeds the default timeout; the session/page closed mid-call.

Common situations: Attribute names built dynamically from test data that contain quotes or whitespace; SPAs re-rendering between query and read; reusing cached handles after page.goto().

Related errors


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

Appendix: source

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

	applySlowMo(h.ctx)

	return nil
}

// GetAttribute retrieves the value of specified element attribute.
// The second return value is true if the attribute exists, and false otherwise.
func (h *ElementHandle) GetAttribute(name string) (string, bool, error) {
	getAttribute := func(apiCtx context.Context, handle *ElementHandle) (any, error) {
		return handle.getAttribute(apiCtx, name)
	}
	opts := NewElementHandleBaseOptions(h.DefaultTimeout())
	getAttributeAction := h.newAction(
		[]string{}, getAttribute, opts.Force, withRetry, opts.NoWaitAfter, opts.Timeout,
	)

	v, err := call(h.ctx, getAttributeAction, opts.Timeout)
	if err != nil {
		return "", false, fmt.Errorf("getting attribute %q of element: %w", name, err)
	}
	if v == nil {
		return "", false, nil
	}
	s, ok := v.(string)
	if !ok {
		return "", false, fmt.Errorf(
			"getting attribute %q of element: unexpected type %T (expecting string)",
			name, v,
		)
	}

	return s, true, nil
}

// Hover scrolls element into view and hovers over its center point.
func (h *ElementHandle) Hover(opts *ElementHandleHoverOptions) error {
	hover := func(apiCtx context.Context, handle *ElementHandle, p *Position) (any, error) {

View on GitHub (pinned to 3fcf5388d7)