grafana/k6 · error
getting text content of element
Error message
getting text content of element: %w
What it means
Thrown by the k6 browser module's ElementHandle.textContent(). It wraps the failure of the underlying textContent action: the evaluation timing out, the element being detached from the DOM, or the CDP evaluation erroring (e.g., execution context destroyed by navigation). Note this is the action-failure branch — a null textContent is returned as ('', false, nil), not an error.
Solutions
- Re-acquire the element after navigation or rerender: el = await page.waitForSelector(selector, { state: 'visible' }).
- Await load state before reading: await page.waitForLoadState('networkidle') or a targeted waitForSelector.
- Wrap in try/catch and retry once with a fresh handle — reading text is idempotent.
- Raise the default timeout via the element's options or browser-level timeout settings.
Example fix
// before
const el = await page.$('#status');
await page.click('#refresh');
const text = await el.textContent(); // stale handle after rerender
// after
await page.click('#refresh');
const el = await page.waitForSelector('#status', { state: 'visible', timeout: '30s' });
const text = await el.textContent(); Defensive patterns
Strategy: try-catch
Try / catch
async function readText(sel) {
for (let i = 0; i < 2; i++) {
try {
const el = await page.waitForSelector(sel, { state: 'visible', timeout: '30s' });
return await el.textContent();
} catch (e) {
if (i === 1) throw e; // retry once with a fresh handle
}
}
} Prevention
- Never read text from a handle obtained before a click/navigation that can rerender the node.
- Re-query with waitForSelector after any state-changing action.
When it happens
Trigger: Calling el.textContent() on a handle whose node was removed by an SPA rerender; calling it while the page navigates and destroys the frame's execution context; with the element's default timeout exceeded on a stalled page.
Common situations: Reading text right after a click that triggers navigation or rerender; heavy pages in CI where evaluation exceeds the default 30s; stale handles kept across page transitions.
Related errors
- checking hit target
- getting element position
- getting injected script
- getting original viewport size
- getting text content of element: unexpected type %T…
AI-assisted analysis of grafana/k6@3fcf5388d7 (2026-08-18).
Data as JSON: /api/errors/3d73640387ddd412.
Report an issue: GitHub.
Appendix: source
Thrown at internal/js/modules/k6/browser/common/element_handle.go:1533
}
func (h *ElementHandle) tap(_ context.Context, p *Position) error {
return h.frame.page.Touchscreen.tap(p.X, p.Y)
}
// TextContent returns the text content of the element.
// The second return value is true if the text content exists, and false otherwise.
func (h *ElementHandle) TextContent() (string, bool, error) {
textContent := func(apiCtx context.Context, handle *ElementHandle) (any, error) {
return handle.textContent(apiCtx)
}
opts := NewElementHandleBaseOptions(h.DefaultTimeout())
textContentAction := h.newAction(
[]string{}, textContent, opts.Force, withRetry, opts.NoWaitAfter, opts.Timeout,
)
v, err := call(h.ctx, textContentAction, opts.Timeout)
if err != nil {
return "", false, fmt.Errorf("getting text content of element: %w", err)
}
if v == nil {
return "", false, nil
}
s, ok := v.(string)
if !ok {
return "", false, fmt.Errorf(
"getting text content of element: unexpected type %T (expecting string)",
v,
)
}
return s, true, nil
}
// Timeout will return the default timeout or the one set by the user.
// It's an internal method not to be exposed as a JS API.
func (h *ElementHandle) Timeout() time.Duration {View on GitHub (pinned to 3fcf5388d7)