{"record":{"id":"59f9d864aaf8c25c","repo":"grafana/k6","slug":"evaluating-handle-for-frame-w","errorCode":null,"errorMessage":"evaluating handle for frame: %w","messagePattern":"evaluating handle for frame: %w","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"internal/js/modules/k6/browser/common/frame.go","lineNumber":919,"sourceCode":"// EvaluateHandle will evaluate provided page function within an execution context.\nfunc (f *Frame) EvaluateHandle(pageFunc string, args ...any) (handle JSHandleAPI, _ error) {\n\tf.log.Debugf(\"Frame:EvaluateHandle\", \"fid:%s furl:%q\", f.ID(), f.URL())\n\n\tevalHandle := func() (JSHandleAPI, error) {\n\t\tf.executionContextMu.RLock()\n\t\tdefer f.executionContextMu.RUnlock()\n\n\t\tec := f.executionContexts[mainWorld]\n\t\tif ec == nil {\n\t\t\treturn nil, fmt.Errorf(\"evaluating handle for frame: execution context %q not found\", mainWorld)\n\t\t}\n\t\treturn ec.EvalHandle(f.ctx, pageFunc, args...) //nolint:wrapcheck\n\t}\n\n\tf.waitForExecutionContext(mainWorld)\n\thandle, err := evalHandle()\n\tif err != nil {\n\t\treturn nil, fmt.Errorf(\"evaluating handle for frame: %w\", err)\n\t}\n\n\tapplySlowMo(f.ctx)\n\n\treturn handle, nil\n}\n\n// Fill fills out the first element found that matches the selector.\nfunc (f *Frame) Fill(selector, value string, popts *FrameFillOptions) error {\n\tf.log.Debugf(\"Frame:Fill\", \"fid:%s furl:%q sel:%q val:%q\", f.ID(), f.URL(), selector, value)\n\n\tif err := f.fill(selector, value, popts); err != nil {\n\t\treturn fmt.Errorf(\"filling %q with %q: %w\", selector, value, err)\n\t}\n\n\tapplySlowMo(f.ctx)\n\n\treturn nil","sourceCodeStart":901,"sourceCodeEnd":937,"githubUrl":"https://github.com/grafana/k6/blob/93accf6570dcd306ca5e99cc44c393ee3797761b/internal/js/modules/k6/browser/common/frame.go#L901-L937","documentation":"Frame.EvaluateHandle() resolves the main-world execution context and calls ec.EvalHandle. This error wraps that call's failure: the page function threw a JS exception, an argument failed to serialize into the context, or the context was destroyed during evaluation.","triggerScenarios":"pageFunc throws at runtime (bad selector inside evaluate, undefined reference); args contain non-serializable values; navigation destroys the context mid-call; invalid function expression syntax.","commonSituations":"Grabbing handles to elements that may not exist yet (querySelector returns null then code dereferences it); passing complex args; evaluating during form-submit navigations.","solutions":["Make the function defensive: null-check before use and run it in DevTools first.","Wait for the elements/state the function depends on (waitForSelector).","Pass only serializable args and use a proper function expression ('() => ...')."],"exampleFix":"// before\nconst h = page.evaluateHandle('() => document.querySelector(\"#x\").dataset'); // #x may be null\n\n// after\nawait page.waitForSelector('#x', { state: 'attached' });\nconst h = page.evaluateHandle('() => document.querySelector(\"#x\").dataset');","handlingStrategy":"try-catch","validationCode":"await frame.waitForSelector(sel, { state: 'attached', timeout: 30000 }); // if pageFunc touches an element","typeGuard":null,"tryCatchPattern":"let h;\ntry {\n  h = frame.evaluateHandle(pageFunc, ...args);\n} catch (e) {\n  console.warn('evaluateHandle failed:', e.message);\n}","preventionTips":["Null-check DOM lookups inside the evaluated function.","Keep args serializable; use function expressions.","Wait for the state the function depends on."],"tags":["browser","evaluate-handle","javascript-exception","serialization","execution-context"],"backgroundTag":null,"analyzedSha":"93accf6570dcd306ca5e99cc44c393ee3797761b","analyzedAt":"2026-08-15T21:23:27.118Z","schemaVersion":2},"datasetVersion":"2026-08-15T22:17:37.221Z"}