{"record":{"id":"3d73640387ddd412","repo":"grafana/k6","slug":"getting-text-content-of-element-w","errorCode":null,"errorMessage":"getting text content of element: %w","messagePattern":"getting text content of element: %w","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"internal/js/modules/k6/browser/common/element_handle.go","lineNumber":1533,"sourceCode":"}\n\nfunc (h *ElementHandle) tap(_ context.Context, p *Position) error {\n\treturn h.frame.page.Touchscreen.tap(p.X, p.Y)\n}\n\n// TextContent returns the text content of the element.\n// The second return value is true if the text content exists, and false otherwise.\nfunc (h *ElementHandle) TextContent() (string, bool, error) {\n\ttextContent := func(apiCtx context.Context, handle *ElementHandle) (any, error) {\n\t\treturn handle.textContent(apiCtx)\n\t}\n\topts := NewElementHandleBaseOptions(h.DefaultTimeout())\n\ttextContentAction := h.newAction(\n\t\t[]string{}, textContent, opts.Force, withRetry, opts.NoWaitAfter, opts.Timeout,\n\t)\n\tv, err := call(h.ctx, textContentAction, opts.Timeout)\n\tif err != nil {\n\t\treturn \"\", false, fmt.Errorf(\"getting text content of element: %w\", err)\n\t}\n\tif v == nil {\n\t\treturn \"\", false, nil\n\t}\n\ts, ok := v.(string)\n\tif !ok {\n\t\treturn \"\", false, fmt.Errorf(\n\t\t\t\"getting text content of element: unexpected type %T (expecting string)\",\n\t\t\tv,\n\t\t)\n\t}\n\n\treturn s, true, nil\n}\n\n// Timeout will return the default timeout or the one set by the user.\n// It's an internal method not to be exposed as a JS API.\nfunc (h *ElementHandle) Timeout() time.Duration {","sourceCodeStart":1515,"sourceCodeEnd":1551,"githubUrl":"https://github.com/grafana/k6/blob/3fcf5388d78cb382f0c0d42ec556a06bfd1b8dee/internal/js/modules/k6/browser/common/element_handle.go#L1515-L1551","documentation":"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.","triggerScenarios":"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.","commonSituations":"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.","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."],"exampleFix":"// before\nconst el = await page.$('#status');\nawait page.click('#refresh');\nconst text = await el.textContent(); // stale handle after rerender\n\n// after\nawait page.click('#refresh');\nconst el = await page.waitForSelector('#status', { state: 'visible', timeout: '30s' });\nconst text = await el.textContent();","handlingStrategy":"try-catch","validationCode":null,"typeGuard":null,"tryCatchPattern":"async function readText(sel) {\n  for (let i = 0; i < 2; i++) {\n    try {\n      const el = await page.waitForSelector(sel, { state: 'visible', timeout: '30s' });\n      return await el.textContent();\n    } catch (e) {\n      if (i === 1) throw e; // retry once with a fresh handle\n    }\n  }\n}","preventionTips":["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."],"tags":["k6","browser","element-handle","text-content","stale-handle"],"backgroundTag":"element-not-attached-to-dom","analyzedSha":"3fcf5388d78cb382f0c0d42ec556a06bfd1b8dee","analyzedAt":"2026-08-18T03:05:52.393Z","contentChangedAt":"2026-08-18T03:05:52.393Z","schemaVersion":2},"datasetVersion":"2026-09-23T08:17:48.524Z"}