{"record":{"id":"48122e051b9bed84","repo":"grafana/k6","slug":"dispatching-frame-event-q-to-q-w","errorCode":null,"errorMessage":"dispatching frame event %q to %q: %w","messagePattern":"dispatching frame event %q to %q: %w","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"internal/js/modules/k6/browser/common/frame.go","lineNumber":826,"sourceCode":"\tdblclick := func(_ context.Context, eh *ElementHandle, p *Position) (any, error) {\n\t\treturn nil, eh.dblclick(p, opts.ToMouseClickOptions())\n\t}\n\tact := f.newPointerAction(\n\t\tselector, DOMElementStateAttached, opts.Strict, dblclick, &opts.ElementHandleBasePointerOptions,\n\t)\n\tif _, err := call(f.ctx, act, opts.Timeout); err != nil {\n\t\treturn errorFromDOMError(err)\n\t}\n\n\treturn nil\n}\n\n// DispatchEvent dispatches an event for the first element matching the selector.\nfunc (f *Frame) DispatchEvent(selector, typ string, eventInit any, opts *FrameDispatchEventOptions) error {\n\tf.log.Debugf(\"Frame:DispatchEvent\", \"fid:%s furl:%q sel:%q typ:%q\", f.ID(), f.URL(), selector, typ)\n\n\tif err := f.dispatchEvent(selector, typ, eventInit, opts); err != nil {\n\t\treturn fmt.Errorf(\"dispatching frame event %q to %q: %w\", typ, selector, err)\n\t}\n\tapplySlowMo(f.ctx)\n\n\treturn nil\n}\n\n// dispatchEvent is like DispatchEvent but takes parsed options and neither throws\n// an error, or applies slow motion.\nfunc (f *Frame) dispatchEvent(selector, typ string, eventInit any, opts *FrameDispatchEventOptions) error {\n\tdispatchEvent := func(apiCtx context.Context, handle *ElementHandle) (any, error) {\n\t\treturn handle.dispatchEvent(apiCtx, typ, eventInit)\n\t}\n\tconst (\n\t\tforce       = false\n\t\tnoWaitAfter = false\n\t)\n\tact := f.newAction(\n\t\tselector, DOMElementStateAttached, opts.Strict, dispatchEvent, []string{},","sourceCodeStart":808,"sourceCodeEnd":844,"githubUrl":"https://github.com/grafana/k6/blob/93accf6570dcd306ca5e99cc44c393ee3797761b/internal/js/modules/k6/browser/common/frame.go#L808-L844","documentation":"Frame.DispatchEvent() waits for an element matching the selector to be attached, then dispatches a DOM event (typ) with the given eventInit on it. This error wraps failures of the wait or the dispatch: selector wait timeout, strict-mode ambiguity, eventInit serialization problems, or the element detaching before dispatch.","triggerScenarios":"Dispatching to a selector that matches nothing within opts.Timeout; eventInit containing non-serializable values (functions, handles); frame navigating during dispatch; multiple strict-mode matches.","commonSituations":"Simulating 'input'/'change' events on fields rendered after async fetches; passing complex eventInit objects; dispatching on elements inside iframes from the main frame.","solutions":["waitForSelector(sel, { state: 'attached' }) before dispatchEvent and raise the timeout.","Keep eventInit to plain serializable data (bubbles, cancelable, detail with primitives).","Verify the selector matches exactly one element in the target frame."],"exampleFix":"// before\nframe.dispatchEvent('#q', 'change', { detail: callback }); // function not serializable\n\n// after\nawait frame.waitForSelector('#q', { state: 'attached' });\nframe.dispatchEvent('#q', 'change', { bubbles: true, detail: { value: 'k6' } });","handlingStrategy":"validation","validationCode":"await frame.waitForSelector(sel, { state: 'attached', timeout: 30000 });\nconst serializable = (v) => { try { JSON.stringify(v); return true; } catch { return false; } }; // check eventInit before passing","typeGuard":null,"tryCatchPattern":"try {\n  frame.dispatchEvent(sel, typ, eventInit, { timeout: 30000 });\n} catch (e) {\n  console.warn(`dispatchEvent(${typ}) on ${sel} failed:`, e.message);\n}","preventionTips":["Keep eventInit to JSON-serializable primitives.","Wait for the target element to attach first.","Verify single match for the selector."],"tags":["browser","dispatchevent","selector","timeout","serialization"],"backgroundTag":null,"analyzedSha":"93accf6570dcd306ca5e99cc44c393ee3797761b","analyzedAt":"2026-08-15T21:23:27.118Z","schemaVersion":2},"datasetVersion":"2026-08-16T03:17:38.424Z"}