grafana/k6 · error

getting element's inner text

Error message

getting element's inner text: %w

What it means

ElementHandle.innerText() wraps the failure of a CDP evaluation returning element.innerText by value. As with innerHTML, the wrapped error indicates the evaluation could not complete: a detached element handle, an execution context destroyed by navigation, a closed frame/session, or an expired timeout.

Solutions

  1. Re-acquire the element right before reading its text
  2. Stabilize the page first: await page.waitForLoadState() or await page.waitForSelector(sel, { state: 'visible' })
  3. Prefer page.locator(sel).innerText() style live queries that re-resolve the element
  4. Guard handle freshness with await el.evaluate(e => e.isConnected)

Example fix

// before
const el = await page.$('#status');
await page.reload();
const text = await el.innerText(); // stale handle

// after
await page.reload();
const text = await page.locator('#status').innerText();
Defensive patterns

Strategy: try-catch

Validate before calling

// Use a live query for text assertions
await page.waitForSelector(sel, { state: 'visible', timeout: '5s' });
const text = await page.locator(sel).innerText();

Try / catch

try {
  const text = await el.innerText();
} catch (e) {
  if (/Could not find object|Execution context|timed out/i.test(String(e.message))) {
    const text = await page.locator(sel).innerText(); // re-resolves the node
  } else throw e;
}

Prevention

When it happens

Trigger: The page navigates or re-renders between query and read; the frame holding the element is detached; the handle came from a previous page; the default timeout expires because the CDP session stalled.

Common situations: Reusing cached handles across navigations; asserting on text of elements inside iframes that get removed; slow browser sessions in containerized CI.

Related errors


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

Appendix: source

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

	if !ok {
		return "", fmt.Errorf("unexpected type %T (expecting string)", v)
	}

	return s, nil
}

// InnerText returns the inner text of the element.
func (h *ElementHandle) InnerText() (string, error) {
	innerText := func(apiCtx context.Context, handle *ElementHandle) (any, error) {
		return handle.innerText(apiCtx)
	}
	opts := NewElementHandleBaseOptions(h.DefaultTimeout())
	innerTextAction := h.newAction(
		[]string{}, innerText, opts.Force, withRetry, opts.NoWaitAfter, opts.Timeout.Abs(),
	)
	v, err := call(h.ctx, innerTextAction, opts.Timeout)
	if err != nil {
		return "", fmt.Errorf("getting element's inner text: %w", err)
	}

	applySlowMo(h.ctx)

	s, ok := v.(string)
	if !ok {
		return "", fmt.Errorf("unexpected type %T (expecting string)", v)
	}

	return s, nil
}

// InputValue returns the value of the input element.
func (h *ElementHandle) InputValue(opts *ElementHandleBaseOptions) (string, error) {
	inputValue := func(apiCtx context.Context, handle *ElementHandle) (any, error) {
		return handle.inputValue(apiCtx)
	}
	inputValueAction := h.newAction([]string{}, inputValue, opts.Force, withRetry, opts.NoWaitAfter, opts.Timeout)

View on GitHub (pinned to 3fcf5388d7)