{"record":{"id":"ac246389415e1c55","repo":"grafana/k6","slug":"evaluating-handle-for-element-w","errorCode":null,"errorMessage":"evaluating handle for element: %w","messagePattern":"evaluating handle for element: %w","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"internal/js/modules/k6/browser/common/js_handle.go","lineNumber":151,"sourceCode":"}\n\n// Evaluate will evaluate provided page function within an execution context.\nfunc (h *BaseJSHandle) Evaluate(pageFunc string, args ...any) (any, error) {\n\targs = append([]any{h}, args...)\n\tres, err := h.execCtx.Eval(h.ctx, pageFunc, args...)\n\tif err != nil {\n\t\treturn nil, fmt.Errorf(\"evaluating element: %w\", err)\n\t}\n\n\treturn res, nil\n}\n\n// EvaluateHandle will evaluate provided page function within an execution context.\nfunc (h *BaseJSHandle) EvaluateHandle(pageFunc string, args ...any) (JSHandleAPI, error) {\n\targs = append([]any{h}, args...)\n\teh, err := h.execCtx.EvalHandle(h.ctx, pageFunc, args...)\n\tif err != nil {\n\t\treturn nil, fmt.Errorf(\"evaluating handle for element: %w\", err)\n\t}\n\n\treturn eh, nil\n}\n\n// GetProperties retreives the JS handle's properties.\nfunc (h *BaseJSHandle) GetProperties() (map[string]JSHandleAPI, error) {\n\thandles, err := h.getProperties()\n\tif err != nil {\n\t\treturn nil, err\n\t}\n\n\tjsHandles := make(map[string]JSHandleAPI, len(handles))\n\tfor k, v := range handles {\n\t\tjsHandles[k] = v\n\t}\n\n\treturn jsHandles, nil","sourceCodeStart":133,"sourceCodeEnd":169,"githubUrl":"https://github.com/grafana/k6/blob/93accf6570dcd306ca5e99cc44c393ee3797761b/internal/js/modules/k6/browser/common/js_handle.go#L133-L169","documentation":"Thrown by BaseJSHandle.EvaluateHandle when the underlying CDP Runtime.evaluate (via execCtx.EvalHandle) fails while running pageFunc with the handle as the first argument. It means the page function could not be evaluated in the handle's execution context: either the function threw a JS exception, or the context/frame was destroyed (navigation, page close) before evaluation completed.","triggerScenarios":"Calling handle.evaluateHandle('h => h.offsetWidth * 2') on a handle whose frame navigated away or was detached; the pageFunction throwing a JavaScript exception at runtime; the browser session or page being closed while evaluation is in flight; using a handle obtained before a page.goto() that replaced the execution context.","commonSituations":"Storing a handle from one page state and using it after a click triggers navigation; long-running scripts where the k6 iteration timeout cancels the context; evaluating code with a syntax error or referencing undefined properties of the handle.","solutions":["Re-acquire the handle right before calling evaluateHandle, after any action that can navigate (click, goto, submit)","Catch the error in the k6 script and retry once after re-querying the element","Verify the page has not navigated (page.url()) and the frame is still attached before evaluating","Test the pageFunc standalone with page.evaluate to isolate a JavaScript exception in the function itself"],"exampleFix":"// before\nconst handle = page.$('#btn');\nawait page.click('#nav');          // navigates, invalidates context\nawait handle.evaluateHandle('h => h.checked');\n\n// after\nawait page.click('#nav');\nawait page.waitForLoadState('load');\nconst handle = await page.$('#btn');   // re-acquire after navigation\nawait handle.evaluateHandle('h => h.checked');","handlingStrategy":"try-catch","validationCode":"const url = page.url(); // ensure page alive, not mid-navigation\nif (page.isClosed()) throw new Error('page closed before evaluateHandle');","typeGuard":null,"tryCatchPattern":"try {\n  const h2 = await handle.evaluateHandle('h => h.nextSibling');\n} catch (e) {\n  if (/evaluating handle/.test(e.message)) {\n    handle = await page.$(selector); // stale context: re-acquire and retry once\n    h2 = await handle.evaluateHandle('h => h.nextSibling');\n  } else throw e;\n}","preventionTips":["Re-acquire handles after any action that can navigate (click, goto, submit)","Do not cache handles across page transitions or iterations","Keep evaluateHandle calls short so they finish well within the timeout"],"tags":["browser","cdp","js-handle","evaluation","stale-context"],"backgroundTag":null,"analyzedSha":"93accf6570dcd306ca5e99cc44c393ee3797761b","analyzedAt":"2026-08-15T21:23:27.118Z","schemaVersion":2},"datasetVersion":"2026-08-15T22:17:37.221Z"}