grafana/k6 · error
getting text content of element: %w
Error message
getting text content of element: %w
What it means
Wrapped failure from ElementHandle.TextContent (elementHandle.textContent()). The inner error comes from the action wrapper executing the textContent evaluation in the page: the element or its frame was detached, the execution context was destroyed by navigation, or the retry loop timed out (default element timeout, since this method uses NewElementHandleBaseOptions(h.DefaultTimeout())).
Source
Thrown at internal/js/modules/k6/browser/common/element_handle.go:1529
}
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 93accf6570)
Solutions
- Re-query the element right before reading: page.waitForSelector(sel, { state: 'attached' }).textContent()
- If reading right after an action that navigates, await page.waitForNavigation() or page.waitForLoadState() first
- Treat the nil-result case explicitly: textContent() returns (null, false, nil) when there is no text — do not confuse that benign case with this error
- Increase the applicable browser timeout option if slowness is systemic
Example fix
// before
const el = page.$('.status');
const text = el.textContent(); // element detached by re-render
// after
const text = page.waitForSelector('.status', { state: 'attached' }).textContent(); Defensive patterns
Strategy: try-catch
Validate before calling
const el = page.waitForSelector('.status', { state: 'attached' });
// reading is then safe; detached handles are the main failure source Type guard
function hasText(result) {
return result === null || typeof result === 'string';
}
// const t = el.textContent(); if (!hasText(t)) throw new Error('unexpected content'); Try / catch
try {
return page.waitForSelector('.status', { state: 'attached' }).textContent();
} catch (e) {
if (String(e).includes('getting text content')) {
return null; // element gone; treat as absent
}
throw e;
} Prevention
- Re-query the element right before reading text
- After clicks that navigate, waitForLoadState before reading
- Remember textContent() returning null is success-with-no-text, not an error
When it happens
Trigger: Calling textContent() on a handle whose element was removed by an SPA re-render; calling it while the page is navigating (context destroyed); exceeding the default 30s timeout on very slow pages because the element never resolves to a stable state.
Common situations: Reading values from list rows that get virtualized/recycled; scraping content immediately after a click that triggers navigation instead of waiting for load; CI environments under load where evaluations are slow.
Related errors
- element is not attached to the DOM
- selecting text: %w
- setting input files: %w
- getting document element: nil document
- clicking the checkbox did not change its state
AI-assisted analysis of grafana/k6@93accf6570 (2026-08-15).
Data as JSON: /api/errors/3d73640387ddd412.
Report an issue: GitHub.