{"record":{"id":"987a07e9bcd4409e","repo":"grafana/k6","slug":"getting-element-s-inner-html-w","errorCode":null,"errorMessage":"getting element's inner HTML: %w","messagePattern":"getting element's inner HTML: %w","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"internal/js/modules/k6/browser/common/element_handle.go","lineNumber":997,"sourceCode":"\t}\n\n\tapplySlowMo(h.ctx)\n\n\treturn nil\n}\n\n// InnerHTML returns the inner HTML of the element.\nfunc (h *ElementHandle) InnerHTML() (string, error) {\n\tinnerHTML := func(apiCtx context.Context, handle *ElementHandle) (any, error) {\n\t\treturn handle.innerHTML(apiCtx)\n\t}\n\topts := NewElementHandleBaseOptions(h.DefaultTimeout())\n\tinnerHTMLAction := h.newAction(\n\t\t[]string{}, innerHTML, opts.Force, withRetry, opts.NoWaitAfter, opts.Timeout,\n\t)\n\tv, err := call(h.ctx, innerHTMLAction, opts.Timeout)\n\tif err != nil {\n\t\treturn \"\", fmt.Errorf(\"getting element's inner HTML: %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// 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())","sourceCodeStart":979,"sourceCodeEnd":1015,"githubUrl":"https://github.com/grafana/k6/blob/3fcf5388d78cb382f0c0d42ec556a06bfd1b8dee/internal/js/modules/k6/browser/common/element_handle.go#L979-L1015","documentation":"ElementHandle.innerHTML() wraps the failure of a CDP evaluation that returns element.innerHTML by value. The read itself is cheap, so the wrapped error almost always means the evaluation could not run: the element handle is detached, the execution context was destroyed by navigation, the session closed, or the default timeout (30s) was exceeded.","triggerScenarios":"The page navigates between acquiring the handle and reading innerHTML; an SPA re-render detaches the node; the frame containing the element was closed; the browser/tab was closed mid-call; the CDP session stalled past the timeout.","commonSituations":"Caching element handles across page.goto() calls; reading DOM state from a page being torn down at iteration end; slow or overloaded browser sessions in CI.","solutions":["Re-acquire the element handle immediately before reading innerHTML","Wait for the page to be in a stable state: page.waitForLoadState() before reading","Guard with await el.evaluate(e => e.isConnected) if the handle's freshness is uncertain","Raise K6_BROWSER_TIMEOUT if the underlying cause is a stalled but recoverable session"],"exampleFix":"// before\nconst el = await page.$('#content');\nawait page.reload();\nconst html = await el.innerHTML(); // stale handle\n\n// after\nawait page.reload();\nconst el = await page.$('#content');\nconst html = await el.innerHTML();","handlingStrategy":"try-catch","validationCode":"// Read DOM state only from fresh, connected handles\nconst connected = await el.evaluate((e) => e.isConnected).catch(() => false);\nif (!connected) throw new Error('stale handle; re-query before innerHTML()');\nconst html = await el.innerHTML();","typeGuard":null,"tryCatchPattern":"try {\n  const html = await el.innerHTML();\n} catch (e) {\n  if (/Could not find object|Execution context|timed out/i.test(String(e.message))) {\n    const html = await page.locator(sel).innerHTML(); // live re-resolve\n  } else throw e;\n}","preventionTips":["Re-acquire handles after navigation or reload before asserting on DOM content","Prefer locators over cached element handles for read-only assertions","Finish DOM reads before triggering teardown (close/popups, navigation)"],"tags":["browser","inner-html","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"}