{"record":{"id":"5094ac7d0918bd1a","repo":"grafana/k6","slug":"getting-element-s-inner-text-w","errorCode":null,"errorMessage":"getting element's inner text: %w","messagePattern":"getting element's inner text: %w","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"internal/js/modules/k6/browser/common/element_handle.go","lineNumber":1021,"sourceCode":"\tif !ok {\n\t\treturn \"\", fmt.Errorf(\"unexpected type %T (expecting string)\", v)\n\t}\n\n\treturn s, nil\n}\n\n// InnerText returns the inner text of the element.\nfunc (h *ElementHandle) InnerText() (string, error) {\n\tinnerText := func(apiCtx context.Context, handle *ElementHandle) (any, error) {\n\t\treturn handle.innerText(apiCtx)\n\t}\n\topts := NewElementHandleBaseOptions(h.DefaultTimeout())\n\tinnerTextAction := h.newAction(\n\t\t[]string{}, innerText, opts.Force, withRetry, opts.NoWaitAfter, opts.Timeout.Abs(),\n\t)\n\tv, err := call(h.ctx, innerTextAction, opts.Timeout)\n\tif err != nil {\n\t\treturn \"\", fmt.Errorf(\"getting element's inner text: %w\", err)\n\t}\n\n\tapplySlowMo(h.ctx)\n\n\ts, ok := v.(string)\n\tif !ok {\n\t\treturn \"\", fmt.Errorf(\"unexpected type %T (expecting string)\", v)\n\t}\n\n\treturn s, nil\n}\n\n// InputValue returns the value of the input element.\nfunc (h *ElementHandle) InputValue(opts *ElementHandleBaseOptions) (string, error) {\n\tinputValue := func(apiCtx context.Context, handle *ElementHandle) (any, error) {\n\t\treturn handle.inputValue(apiCtx)\n\t}\n\tinputValueAction := h.newAction([]string{}, inputValue, opts.Force, withRetry, opts.NoWaitAfter, opts.Timeout)","sourceCodeStart":1003,"sourceCodeEnd":1039,"githubUrl":"https://github.com/grafana/k6/blob/3fcf5388d78cb382f0c0d42ec556a06bfd1b8dee/internal/js/modules/k6/browser/common/element_handle.go#L1003-L1039","documentation":"ElementHandle.innerText() wraps the failure of a CDP evaluation returning element.innerText by value. As with innerHTML, the wrapped error indicates the evaluation could not complete: a detached element handle, an execution context destroyed by navigation, a closed frame/session, or an expired timeout.","triggerScenarios":"The page navigates or re-renders between query and read; the frame holding the element is detached; the handle came from a previous page; the default timeout expires because the CDP session stalled.","commonSituations":"Reusing cached handles across navigations; asserting on text of elements inside iframes that get removed; slow browser sessions in containerized CI.","solutions":["Re-acquire the element right before reading its text","Stabilize the page first: await page.waitForLoadState() or await page.waitForSelector(sel, { state: 'visible' })","Prefer page.locator(sel).innerText() style live queries that re-resolve the element","Guard handle freshness with await el.evaluate(e => e.isConnected)"],"exampleFix":"// before\nconst el = await page.$('#status');\nawait page.reload();\nconst text = await el.innerText(); // stale handle\n\n// after\nawait page.reload();\nconst text = await page.locator('#status').innerText();","handlingStrategy":"try-catch","validationCode":"// Use a live query for text assertions\nawait page.waitForSelector(sel, { state: 'visible', timeout: '5s' });\nconst text = await page.locator(sel).innerText();","typeGuard":null,"tryCatchPattern":"try {\n  const text = await el.innerText();\n} catch (e) {\n  if (/Could not find object|Execution context|timed out/i.test(String(e.message))) {\n    const text = await page.locator(sel).innerText(); // re-resolves the node\n  } else throw e;\n}","preventionTips":["Do not cache element handles across page.goto()/reload()","Read text immediately after waitForSelector confirms the element","Keep read operations away from page teardown races at iteration end"],"tags":["browser","inner-text","dom","stale-element","execution-context","k6"],"backgroundTag":"stale-element-reference","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"}