{"record":{"id":"d476d0f5ece26eb1","repo":"grafana/k6","slug":"dispatching-element-event-q-w","errorCode":null,"errorMessage":"dispatching element event %q: %w","messagePattern":"dispatching element event %q: %w","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"internal/js/modules/k6/browser/common/element_handle.go","lineNumber":898,"sourceCode":"\t\treturn fmt.Errorf(\"double clicking on element: %w\", err)\n\t}\n\n\tapplySlowMo(h.ctx)\n\n\treturn nil\n}\n\n// DispatchEvent dispatches a DOM event to the element.\nfunc (h *ElementHandle) DispatchEvent(typ string, eventInit any) error {\n\tdispatchEvent := func(apiCtx context.Context, handle *ElementHandle) (any, error) {\n\t\treturn handle.dispatchEvent(apiCtx, typ, eventInit)\n\t}\n\topts := NewElementHandleBaseOptions(h.DefaultTimeout())\n\tdispatchEventAction := h.newAction(\n\t\t[]string{}, dispatchEvent, opts.Force, withRetry, opts.NoWaitAfter, opts.Timeout,\n\t)\n\tif _, err := call(h.ctx, dispatchEventAction, opts.Timeout); err != nil {\n\t\treturn fmt.Errorf(\"dispatching element event %q: %w\", typ, err)\n\t}\n\n\tapplySlowMo(h.ctx)\n\n\treturn nil\n}\n\n// Fill types the given value into the element.\nfunc (h *ElementHandle) Fill(value string, opts *ElementHandleBaseOptions) error {\n\tfill := func(apiCtx context.Context, handle *ElementHandle) (any, error) {\n\t\treturn nil, handle.fill(apiCtx, value)\n\t}\n\tfillAction := h.newAction(\n\t\t[]string{\"visible\", \"enabled\", \"editable\"},\n\t\tfill, opts.Force, withRetry, opts.NoWaitAfter, opts.Timeout,\n\t)\n\tif _, err := call(h.ctx, fillAction, opts.Timeout); err != nil {\n\t\treturn fmt.Errorf(\"filling element: %w\", err)","sourceCodeStart":880,"sourceCodeEnd":916,"githubUrl":"https://github.com/grafana/k6/blob/93accf6570dcd306ca5e99cc44c393ee3797761b/internal/js/modules/k6/browser/common/element_handle.go#L880-L916","documentation":"DispatchEvent wraps the injected dispatchEvent evaluation, run as a retrying action under the default element timeout. Failures include eval errors (element detached, context destroyed), non-serializable eventInit payloads, and timeouts.","triggerScenarios":"handle.dispatchEvent('click', eventInit) where eventInit contains non-JSON-serializable values, the element detaches before dispatch, the execution context dies, or the action exceeds the timeout.","commonSituations":"Dispatching custom events on dynamically removed nodes; passing functions/DOM nodes/class instances in eventInit; wrong event type strings; slow pages exceeding the default timeout.","solutions":["Ensure eventInit is a plain serializable object (strings, numbers, booleans, arrays, nested objects)","Re-locate the element right before dispatching","Use a valid event type name (e.g. 'click', 'submit', 'custom-event')","Increase the timeout if the page is slow"],"exampleFix":"// before\nawait handle.dispatchEvent('search', { query: () => 'k6' }); // dispatching element event\n\n// after\nawait handle.dispatchEvent('search', { detail: { query: 'k6' } });","handlingStrategy":"try-catch","validationCode":"// eventInit must be plain serializable data\nfunction isPlainSerializable(v) {\n  if (v === null) return true;\n  const t = typeof v;\n  if (['string', 'number', 'boolean'].includes(t)) return true;\n  if (t !== 'object') return false;\n  return Object.values(v).every(isPlainSerializable);\n}\nif (!isPlainSerializable(eventInit)) throw new Error('eventInit not serializable');","typeGuard":null,"tryCatchPattern":"try {\n  await handle.dispatchEvent('click', { bubbles: true });\n} catch (e) {\n  if (String(e).includes('dispatching element event')) {\n    await (await page.$(sel)).dispatchEvent('click', { bubbles: true });\n  } else { throw e; }\n}","preventionTips":["Pass only JSON-safe values in eventInit","Re-locate elements before dispatching on dynamic nodes","Use standard event type names"],"tags":["browser","dispatch-event","serialization","element-handle"],"backgroundTag":null,"analyzedSha":"93accf6570dcd306ca5e99cc44c393ee3797761b","analyzedAt":"2026-08-15T21:23:27.118Z","schemaVersion":2},"datasetVersion":"2026-08-15T22:17:37.221Z"}