{"record":{"id":"a5d107e2af88d02e","repo":"grafana/k6","slug":"getting-attribute-q-of-element-unexpected-type","errorCode":null,"errorMessage":"getting attribute %q of element: unexpected type %T (expecting string)","messagePattern":"getting attribute %q 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":962,"sourceCode":"func (h *ElementHandle) GetAttribute(name string) (string, bool, error) {\n\tgetAttribute := func(apiCtx context.Context, handle *ElementHandle) (any, error) {\n\t\treturn handle.getAttribute(apiCtx, name)\n\t}\n\topts := NewElementHandleBaseOptions(h.DefaultTimeout())\n\tgetAttributeAction := h.newAction(\n\t\t[]string{}, getAttribute, opts.Force, withRetry, opts.NoWaitAfter, opts.Timeout,\n\t)\n\n\tv, err := call(h.ctx, getAttributeAction, opts.Timeout)\n\tif err != nil {\n\t\treturn \"\", false, fmt.Errorf(\"getting attribute %q of element: %w\", name, 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 attribute %q of element: unexpected type %T (expecting string)\",\n\t\t\tname, v,\n\t\t)\n\t}\n\n\treturn s, true, nil\n}\n\n// Hover scrolls element into view and hovers over its center point.\nfunc (h *ElementHandle) Hover(opts *ElementHandleHoverOptions) error {\n\thover := func(apiCtx context.Context, handle *ElementHandle, p *Position) (any, error) {\n\t\treturn nil, handle.hover(apiCtx, p)\n\t}\n\thoverAction := h.newPointerAction(hover, &opts.ElementHandleBasePointerOptions)\n\tif _, err := call(h.ctx, hoverAction, opts.Timeout); err != nil {\n\t\treturn fmt.Errorf(\"hovering on element: %w\", err)\n\t}\n","sourceCodeStart":944,"sourceCodeEnd":980,"githubUrl":"https://github.com/grafana/k6/blob/93accf6570dcd306ca5e99cc44c393ee3797761b/internal/js/modules/k6/browser/common/element_handle.go#L944-L980","documentation":"Internal invariant failure inside ElementHandle.getAttribute(name): the CDP evaluation succeeded but the returned remote value did not type-assert to a Go string. DOM attribute values are strings by spec (or null, which is handled earlier), so a non-string payload means an unexpected protocol serialization or a k6 bug, not a normal scripting mistake.","triggerScenarios":"Only produced when the browser returns a non-string RemoteObject for the attribute evaluation - typically a Chromium/CDP version mismatch with the k6 release, or a regression in the injected evaluation. Not reproducible through ordinary script inputs.","commonSituations":"Running a much newer or canary Chromium against an older k6; custom browser binaries via K6_BROWSER_EXECUTABLE_PATH; after a k6 upgrade changed the evaluation plumbing.","solutions":["Upgrade k6 to the latest release - the browser module tracks Chromium versions closely","Pin the browser binary to a version known to work with your k6 release","Work around it by reading the attribute through evaluate: page.evaluate(el => el.getAttribute(name), handle)","Report the issue to k6 with the script, k6 version, and Chromium version"],"exampleFix":"// before\nconst [v, ok] = await h.getAttribute('data-id');\n// after (workaround via evaluate)\nconst v = await h.evaluate((el) => el.getAttribute('data-id'));\nif (typeof v !== 'string') { /* treat as missing */ }","handlingStrategy":"validation","validationCode":"const v = await handle.evaluate((el, name) => el.getAttribute(name), name);\nif (typeof v === 'string') {\n  // safe to use\n}","typeGuard":"function isAttrString(v) {\n  return typeof v === 'string';\n}","tryCatchPattern":"try {\n  const [v, ok] = await handle.getAttribute(name);\n} catch (e) {\n  if (String(e).includes('unexpected type')) {\n    return handle.evaluate((el) => el.getAttribute(name), name);\n  }\n  throw e;\n}","preventionTips":["Keep k6 and its bundled/pinned Chromium versions in lockstep","Avoid canary browser builds in CI","Prefer evaluate()-based attribute reads if a version mismatch is suspected"],"tags":["browser","internal","type-assertion","compatibility"],"backgroundTag":null,"analyzedSha":"93accf6570dcd306ca5e99cc44c393ee3797761b","analyzedAt":"2026-08-15T21:23:27.118Z","schemaVersion":2},"datasetVersion":"2026-08-15T22:17:37.221Z"}