grafana/k6 · error

getting element's inner HTML: %w

Error message

getting element's inner HTML: %w

What it means

Returned by ElementHandle.innerHTML() when the underlying evaluation fails. The method evaluates element.innerHTML via CDP with returnByValue inside a retried action; this error wraps CDP/transport failures, stale handles, destroyed execution contexts (navigation), or an expired timeout.

Source

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

	}

	applySlowMo(h.ctx)

	return nil
}

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

	applySlowMo(h.ctx)

	s, ok := v.(string)
	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())

View on GitHub (pinned to 93accf6570)

Solutions

  1. Wait for the element: page.waitForSelector(sel, { state: 'attached' })
  2. Re-query the handle immediately before innerHTML()
  3. After a navigation-triggering action, await page.waitForLoadState() first
  4. Increase the timeout if the wrapped error indicates a timeout

Example fix

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

Strategy: try-catch

Validate before calling

await page.waitForSelector('#content', { state: 'attached' });
const el = await page.$('#content');
if (el) {
  const html = await el.innerHTML();
}

Try / catch

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

Prevention

When it happens

Trigger: Calling innerHTML() on a detached or pre-navigation handle; calling it while the page navigates and the execution context is destroyed; the action timeout expiring; the browser/page being closed mid-evaluation.

Common situations: Scraping rendered HTML from SPAs that re-render between page.$() and innerHTML(); reading HTML right after a click that triggers navigation; tight timeouts on slow CI browsers.

Related errors


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