grafana/k6 · error

getting element's inner text: %w

Error message

getting element's inner text: %w

What it means

Returned by ElementHandle.innerText() when the underlying evaluation fails. The method evaluates element.innerText via CDP with returnByValue (note it passes opts.Timeout.Abs() to the action); this error wraps CDP failures, stale handles, destroyed execution contexts, or an expired timeout.

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 93accf6570)

Solutions

  1. Wait for the element: page.waitForSelector(sel, { state: 'attached' })
  2. Re-query the handle right before innerText()
  3. Await page.waitForLoadState() after navigations before reading text
  4. Increase the action/browser timeout if the wrapped cause is a timeout

Example fix

// before
const text = await (await page.$('#msg')).innerText();
// after
await page.waitForSelector('#msg', { state: 'attached' });
const el = await page.$('#msg');
const text = await el.innerText();
Defensive patterns

Strategy: try-catch

Validate before calling

await page.waitForSelector('#msg', { state: 'attached' });
const el = await page.$('#msg');
if (el) {
  const text = await el.innerText();
}

Try / catch

try {
  return await handle.innerText();
} catch (e) {
  if (String(e).includes("getting element's inner text")) {
    await page.waitForLoadState();
    return (await page.$(sel)).innerText();
  }
  throw e;
}

Prevention

When it happens

Trigger: Calling innerText() on a detached handle or one captured before navigation; the page navigating while the evaluation runs; the action timeout expiring; the browser/page closing mid-call.

Common situations: Extracting visible text from re-rendering SPA lists; reading text right after a click that navigates; slow CI browsers hitting the default timeout.

Related errors


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