{"record":{"id":"7a261c247cf3fb1b","repo":"grafana/k6","slug":"focusing-q-w","errorCode":null,"errorMessage":"focusing %q: %w","messagePattern":"focusing %q: %w","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"internal/js/modules/k6/browser/common/frame.go","lineNumber":961,"sourceCode":"\t}\n\tact := f.newAction(\n\t\tselector, DOMElementStateAttached, opts.Strict,\n\t\tfill, []string{\"visible\", \"enabled\", \"editable\"},\n\t\topts.Force, withRetry, opts.NoWaitAfter, opts.Timeout,\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// Focus focuses on the first element that matches the selector.\nfunc (f *Frame) Focus(selector string, popts *FrameBaseOptions) error {\n\tf.log.Debugf(\"Frame:Focus\", \"fid:%s furl:%q sel:%q\", f.ID(), f.URL(), selector)\n\n\tif err := f.focus(selector, popts); err != nil {\n\t\treturn fmt.Errorf(\"focusing %q: %w\", selector, err)\n\t}\n\n\tapplySlowMo(f.ctx)\n\n\treturn nil\n}\n\nfunc (f *Frame) focus(selector string, opts *FrameBaseOptions) error {\n\tfocus := func(apiCtx context.Context, handle *ElementHandle) (any, error) {\n\t\treturn nil, handle.focus(apiCtx, true)\n\t}\n\tact := f.newAction(\n\t\tselector, DOMElementStateAttached, opts.Strict, focus,\n\t\t[]string{}, false, withRetry, true, opts.Timeout,\n\t)\n\tif _, err := call(f.ctx, act, opts.Timeout); err != nil {\n\t\treturn errorFromDOMError(err)\n\t}","sourceCodeStart":943,"sourceCodeEnd":979,"githubUrl":"https://github.com/grafana/k6/blob/93accf6570dcd306ca5e99cc44c393ee3797761b/internal/js/modules/k6/browser/common/frame.go#L943-L979","documentation":"Frame.Focus() runs an action that waits for an element matching the selector to be attached and visible, then focuses it. This error wraps failures of that pipeline — typically 'waiting for selector ... timed out' or 'waiting for selector %q did not result in any nodes' when nothing matches, or DOM errors like element-not-connected.","triggerScenarios":"Selector matches nothing/hidden element within opts.Timeout; strict-mode multiple matches; element detached between the wait and the focus call.","commonSituations":"Focusing fields rendered after async loads; focusing elements in the wrong frame; modals that steal focus while the action runs; shortened timeouts in CI.","solutions":["Wait for visible state first: await frame.waitForSelector(sel, { state: 'visible' }).","Use a unique selector and increase the timeout option.","Target the correct frame for iframe-embedded fields."],"exampleFix":"// before\nframe.focus('#search', { timeout: 1000 });\n\n// after\nawait frame.waitForSelector('#search', { state: 'visible', timeout: 30000 });\nframe.focus('#search', { timeout: 30000 });","handlingStrategy":"validation","validationCode":"await frame.waitForSelector(sel, { state: 'visible', timeout: 30000 });","typeGuard":null,"tryCatchPattern":"try {\n  frame.focus(sel, { timeout: 30000 });\n} catch (e) {\n  console.warn(`focus(${sel}) failed:`, e.message);\n}","preventionTips":["Wait for visible state before focusing.","Use unique selectors and explicit timeouts.","Resolve the correct frame for iframe fields."],"tags":["browser","focus","selector","timeout","wait"],"backgroundTag":null,"analyzedSha":"93accf6570dcd306ca5e99cc44c393ee3797761b","analyzedAt":"2026-08-15T21:23:27.118Z","schemaVersion":2},"datasetVersion":"2026-08-15T22:17:37.221Z"}