{"record":{"id":"283b239218a88931","repo":"grafana/k6","slug":"getting-text-content-of-element-unexpected-type","errorCode":null,"errorMessage":"getting text content of element: unexpected type %T (expecting string)","messagePattern":"getting text content of element: unexpected type %T \\(expecting string\\)","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"internal/js/modules/k6/browser/common/element_handle.go","lineNumber":1540,"sourceCode":"// 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 {\n\treturn h.DefaultTimeout()\n}\n\n// Type scrolls element into view, focuses element and types text.\nfunc (h *ElementHandle) Type(text string, opts *ElementHandleTypeOptions) error {\n\ttyp := func(apiCtx context.Context, handle *ElementHandle) (any, error) {\n\t\treturn nil, handle.typ(apiCtx, text, KeyboardOptions{})","sourceCodeStart":1522,"sourceCodeEnd":1558,"githubUrl":"https://github.com/grafana/k6/blob/3fcf5388d78cb382f0c0d42ec556a06bfd1b8dee/internal/js/modules/k6/browser/common/element_handle.go#L1522-L1558","documentation":"Internal invariant error in the k6 browser module's ElementHandle.textContent(). The action succeeded and returned a non-nil value, but it was not a Go string (e.g., a sobek.Value or remote-object wrapper). Per the DOM spec element.textContent is string or null, and null is handled separately, so hitting this branch means an unexpected CDP/sobek serialization — an internal/module-level anomaly rather than a script mistake.","triggerScenarios":"CDP returning a value wrapper the module did not serialize to string (returnByValue path anomaly); version skew between k6 and its embedded browser module; a bug in the module's eval plumbing.","commonSituations":"Rare; typically after upgrading k6 to a version with a regression, or when a custom k6 build mixes browser module versions.","solutions":["Upgrade (or roll back) k6 to a known-good release — this is a module defect, not a page condition.","As a workaround, read the text via evaluation: const text = await el.eval('el => el.textContent').","Report to the k6-browser repo with the k6 version and a minimal reproducer."],"exampleFix":"// workaround: bypass textContent()'s internal conversion\nconst el = await page.waitForSelector('#status');\nconst text = await el.eval('el => el.textContent');","handlingStrategy":"fallback","validationCode":null,"typeGuard":null,"tryCatchPattern":"try {\n  text = await el.textContent();\n} catch (e) {\n  if (String(e).includes('unexpected type')) {\n    text = await el.eval('n => n.textContent'); // bypass internal conversion\n  } else { throw e; }\n}","preventionTips":["Treat this error as a module/version defect: reproduce, then pin or upgrade k6.","Have an el.eval fallback in shared helpers for text extraction."],"tags":["k6","browser","element-handle","internal","unexpected-type"],"backgroundTag":"unexpected-return-type","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"}