{"record":{"id":"79057b393a6dfa7f","repo":"grafana/k6","slug":"unexpected-type-t-expecting-string","errorCode":null,"errorMessage":"unexpected type %T (expecting string)","messagePattern":"unexpected type %T \\(expecting string\\)","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"internal/js/modules/k6/browser/common/element_handle.go","lineNumber":1004,"sourceCode":"// 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())\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}","sourceCodeStart":986,"sourceCodeEnd":1022,"githubUrl":"https://github.com/grafana/k6/blob/3fcf5388d78cb382f0c0d42ec556a06bfd1b8dee/internal/js/modules/k6/browser/common/element_handle.go#L986-L1022","documentation":"After the innerHTML evaluation succeeds, k6 asserts the CDP result is a Go string; anything non-nil and non-string raises this invariant error. element.innerHTML is a string for real elements, so this fires when the handle points at a node without that property (text node, comment node) or when page scripts tamper with the DOM.","triggerScenarios":"The selector/handle resolved to a text or comment node rather than an element (e.g., a handle obtained from evaluate returning a node); SVG/MathML edge cases; pages overriding innerHTML via Object.defineProperty on prototypes.","commonSituations":"Handles produced by el.evaluateHandle(...) expressions that return nodes; XPath selectors matching text nodes; instrumented pages that redefine DOM properties. Rare on stock pages.","solutions":["Make sure the selector matches an element node: check await el.evaluate(e => e.nodeType === Node.ELEMENT_NODE)","Re-read via evaluation with a String() coercion: await el.evaluate(e => String(e.innerHTML))","Use textContent() instead if you only need the node's content"],"exampleFix":"// before\nconst html = await el.innerHTML(); // handle is a text node\n\n// after\nif (!(await el.evaluate(e => e.nodeType === Node.ELEMENT_NODE))) {\n  throw new Error('handle is not an element node');\n}\nconst html = await el.innerHTML();","handlingStrategy":"validation","validationCode":"// Confirm the handle is an element node before innerHTML()\nconst isElement = await el.evaluate((e) => e.nodeType === Node.ELEMENT_NODE);\nif (!isElement) {\n  throw new Error('handle is not an element node; innerHTML is undefined');\n}\nconst html = await el.innerHTML();","typeGuard":null,"tryCatchPattern":"try {\n  const html = await el.innerHTML();\n} catch (e) {\n  if (/unexpected type/i.test(String(e.message))) {\n    const html = await el.evaluate((e2) => String(e2.innerHTML ?? ''));\n  } else throw e;\n}","preventionTips":["Avoid evaluateHandle()-derived handles for innerHTML(); use selectors that match elements","Check nodeType when handles come from XPath expressions that can match text nodes"],"tags":["browser","inner-html","type-assertion","dom","k6"],"backgroundTag":"type-assertion-failed","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"}