{"record":{"id":"4e0c6ad3c2ac8199","repo":"grafana/k6","slug":"evaluating-element-w","errorCode":null,"errorMessage":"evaluating element: %w","messagePattern":"evaluating element: %w","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"internal/js/modules/k6/browser/common/js_handle.go","lineNumber":140,"sourceCode":"\th.disposed = true\n\tif h.remoteObject.ObjectID == \"\" {\n\t\treturn nil\n\t}\n\tact := runtime.ReleaseObject(h.remoteObject.ObjectID)\n\tif err := act.Do(cdp.WithExecutor(h.ctx, h.session)); err != nil {\n\t\treturn fmt.Errorf(\"disposing element with ID %s: %w\",\n\t\t\th.remoteObject.ObjectID, err)\n\t}\n\n\treturn nil\n}\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) {","sourceCodeStart":122,"sourceCodeEnd":158,"githubUrl":"https://github.com/grafana/k6/blob/93accf6570dcd306ca5e99cc44c393ee3797761b/internal/js/modules/k6/browser/common/js_handle.go#L122-L158","documentation":"BaseJSHandle.Evaluate prepends the handle itself as the first argument and runs your page function in the handle's execution context via execCtx.Eval. This error wraps any failure from that evaluation: a JavaScript exception thrown inside the function, the execution context having been destroyed by navigation, an argument conversion failure, or a session problem.","triggerScenarios":"handle.evaluate(fn) where fn throws at runtime (property access on a stale/detached element, null returned from a lookup), where the handle's execution context was destroyed by navigation or iframe replacement, or where arguments fail conversion before the call.","commonSituations":"Reusing element handles after page navigation or DOM re-renders (classic stale-handle problem); evaluating on elements inside frames that reloaded; functions that assume a property exists; evaluate racing a route-fulfilled navigation.","solutions":["Re-query the element right before evaluate instead of reusing stale handles","Make the evaluated function defensive (optional chaining, null checks) so it does not throw on missing nodes","Wait for a stable state (page.waitForLoadState, waitForSelector) before evaluating","Retry once with a freshly queried handle if the failure was caused by navigation"],"exampleFix":"// before\nconst btn = await page.$('#submit');\nawait page.goto(url2);\nawait btn.evaluate((b) => b.textContent.length); // context destroyed\n\n// after\nawait page.goto(url2);\nconst btn = await page.$('#submit');\nawait btn.evaluate((b) => b?.textContent?.length ?? 0);","handlingStrategy":"try-catch","validationCode":"// re-query right before evaluating, and prefer locator-style flows\nconst btn = await page.$('#submit');\nconst label = await btn.evaluate((b) => b?.textContent ?? '');","typeGuard":null,"tryCatchPattern":"try {\n  result = await handle.evaluate(fn);\n} catch (e) {\n  if (!/evaluating element/.test(String(e.message))) throw e;\n  const fresh = await page.$(selector); // re-query after navigation\n  result = await fresh.evaluate(fn);\n}","preventionTips":["Re-query element handles after any navigation instead of reusing them","Write defensive page functions (optional chaining, null defaults)","Wait for load/selector stability before evaluating on handles"],"tags":["browser","evaluate","js-handle","stale-reference","execution-context"],"backgroundTag":null,"analyzedSha":"93accf6570dcd306ca5e99cc44c393ee3797761b","analyzedAt":"2026-08-15T21:23:27.118Z","schemaVersion":2},"datasetVersion":"2026-08-16T03:17:38.424Z"}