{"record":{"id":"06831ab24d721f44","repo":"grafana/k6","slug":"getting-element-handle-for-selector-q-w","errorCode":null,"errorMessage":"getting element handle for selector %q: %w","messagePattern":"getting element handle for selector %q: %w","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"internal/js/modules/k6/browser/common/element_handle.go","lineNumber":1281,"sourceCode":"\t\treturn nil, fmt.Errorf(\"parsing selector %q: %w\", selector, err)\n\t}\n\tresult, err := eval(\n\t\th.ctx,\n\t\tevalOptions{forceCallable: true, returnByValue: false},\n\t\tjs.QueryAll,\n\t\tparsedSelector,\n\t)\n\tif err != nil {\n\t\treturn nil, fmt.Errorf(\"querying all selectors %q: %w\", selector, err)\n\t}\n\tif result == nil {\n\t\t// it is ok to return a nil slice because it means we didn't find any elements.\n\t\treturn nil, nil\n\t}\n\n\thandles, ok := result.(JSHandleAPI)\n\tif !ok {\n\t\treturn nil, fmt.Errorf(\"getting element handle for selector %q: %w\", selector, ErrJSHandleInvalid)\n\t}\n\tdefer func() {\n\t\tif err := handles.Dispose(); err != nil {\n\t\t\terr = fmt.Errorf(\"disposing element handles: %w\", err)\n\t\t\trerr = errors.Join(err, rerr)\n\t\t}\n\t}()\n\n\tprops, err := handles.GetProperties()\n\tif err != nil {\n\t\t// GetProperties has a rich error already, so we don't need to wrap it.\n\t\treturn nil, err //nolint:wrapcheck\n\t}\n\n\ttype indexedElem struct {\n\t\tindex int\n\t\telem  *ElementHandle\n\t}","sourceCodeStart":1263,"sourceCodeEnd":1299,"githubUrl":"https://github.com/grafana/k6/blob/93accf6570dcd306ca5e99cc44c393ee3797761b/internal/js/modules/k6/browser/common/element_handle.go#L1263-L1299","documentation":"queryAll type-asserted the evaluation result to JSHandleAPI and failed, wrapping the sentinel ErrJSHandleInvalid ('JS handle is invalid', errors.go:23) with the selector (element_handle.go:1281). The protocol result was not a materialized object handle — an internal invariant break or an invalid handle from a destroyed context, not a selector problem.","triggerScenarios":"A chromium/CDP version returning an unexpected result shape for the injected script; evaluation resolving after the context was destroyed so the handle is invalid; k6 browser-module bug.","commonSituations":"Rare and typically version-correlated (after a chromium bump) or load-correlated (context teardown races).","solutions":["Upgrade k6 to the latest version","If reproducible, capture the script + versions and file a k6 issue","Work around using page.$$ on the page or frame instead of element.$$","Retry once — a context-teardown race produces this only transiently"],"exampleFix":null,"handlingStrategy":"try-catch","validationCode":null,"typeGuard":null,"tryCatchPattern":"try { return await el.$$(sel); }\ncatch (e) { if (/JS handle is invalid/.test(e.message)) { return await page.$$(sel); /* page-level fallback */ } throw e; }","preventionTips":["Keep k6/chromium versions in sync","Use page.$$ as a fallback path when element.$$ hits the invariant","Report reproducible occurrences upstream"],"tags":["browser","internal","cdp","invariant"],"backgroundTag":null,"analyzedSha":"93accf6570dcd306ca5e99cc44c393ee3797761b","analyzedAt":"2026-08-15T21:23:27.118Z","schemaVersion":2},"datasetVersion":"2026-08-15T22:17:37.221Z"}