grafana/k6 · error
evaluating handle for element: %w
Error message
evaluating handle for element: %w
What it means
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.
Source
Thrown at internal/js/modules/k6/browser/common/js_handle.go:151
}
// Evaluate will evaluate provided page function within an execution context.
func (h *BaseJSHandle) Evaluate(pageFunc string, args ...any) (any, error) {
args = append([]any{h}, args...)
res, err := h.execCtx.Eval(h.ctx, pageFunc, args...)
if err != nil {
return nil, fmt.Errorf("evaluating element: %w", err)
}
return res, nil
}
// EvaluateHandle will evaluate provided page function within an execution context.
func (h *BaseJSHandle) EvaluateHandle(pageFunc string, args ...any) (JSHandleAPI, error) {
args = append([]any{h}, args...)
eh, err := h.execCtx.EvalHandle(h.ctx, pageFunc, args...)
if err != nil {
return nil, fmt.Errorf("evaluating handle for element: %w", err)
}
return eh, nil
}
// GetProperties retreives the JS handle's properties.
func (h *BaseJSHandle) GetProperties() (map[string]JSHandleAPI, error) {
handles, err := h.getProperties()
if err != nil {
return nil, err
}
jsHandles := make(map[string]JSHandleAPI, len(handles))
for k, v := range handles {
jsHandles[k] = v
}
return jsHandles, nilView on GitHub (pinned to 93accf6570)
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
Example fix
// before
const handle = page.$('#btn');
await page.click('#nav'); // navigates, invalidates context
await handle.evaluateHandle('h => h.checked');
// after
await page.click('#nav');
await page.waitForLoadState('load');
const handle = await page.$('#btn'); // re-acquire after navigation
await handle.evaluateHandle('h => h.checked'); Defensive patterns
Strategy: try-catch
Validate before calling
const url = page.url(); // ensure page alive, not mid-navigation
if (page.isClosed()) throw new Error('page closed before evaluateHandle'); Try / catch
try {
const h2 = await handle.evaluateHandle('h => h.nextSibling');
} catch (e) {
if (/evaluating handle/.test(e.message)) {
handle = await page.$(selector); // stale context: re-acquire and retry once
h2 = await handle.evaluateHandle('h => h.nextSibling');
} else throw e;
} Prevention
- 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
When it happens
Trigger: 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.
Common situations: 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.
Related errors
- adding script to evaluate on new document: %w
- disposing element with ID %s: %w
- getting properties for element with ID %s: %w
- retrieving json value: %w
- WebSocket endpoint cannot be empty
AI-assisted analysis of grafana/k6@93accf6570 (2026-08-15).
Data as JSON: /api/errors/ac246389415e1c55.
Report an issue: GitHub.