{"record":{"id":"3392106af78fbebe","repo":"grafana/k6","slug":"focusing-on-element-w","errorCode":null,"errorMessage":"focusing on element: %w","messagePattern":"focusing on element: %w","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"internal/js/modules/k6/browser/common/element_handle.go","lineNumber":934,"sourceCode":"\t\treturn fmt.Errorf(\"filling element: %w\", err)\n\t}\n\n\tapplySlowMo(h.ctx)\n\n\treturn nil\n}\n\n// Focus scrolls element into view and focuses the element.\nfunc (h *ElementHandle) Focus() error {\n\tfocus := func(apiCtx context.Context, handle *ElementHandle) (any, error) {\n\t\treturn nil, handle.focus(apiCtx, false)\n\t}\n\topts := NewElementHandleBaseOptions(h.DefaultTimeout())\n\tfocusAction := h.newAction(\n\t\t[]string{}, focus, opts.Force, withRetry, opts.NoWaitAfter, opts.Timeout,\n\t)\n\tif _, err := call(h.ctx, focusAction, opts.Timeout); err != nil {\n\t\treturn fmt.Errorf(\"focusing on element: %w\", err)\n\t}\n\n\tapplySlowMo(h.ctx)\n\n\treturn nil\n}\n\n// GetAttribute retrieves the value of specified element attribute.\n// The second return value is true if the attribute exists, and false otherwise.\nfunc (h *ElementHandle) GetAttribute(name string) (string, bool, error) {\n\tgetAttribute := func(apiCtx context.Context, handle *ElementHandle) (any, error) {\n\t\treturn handle.getAttribute(apiCtx, name)\n\t}\n\topts := NewElementHandleBaseOptions(h.DefaultTimeout())\n\tgetAttributeAction := h.newAction(\n\t\t[]string{}, getAttribute, opts.Force, withRetry, opts.NoWaitAfter, opts.Timeout,\n\t)\n","sourceCodeStart":916,"sourceCodeEnd":952,"githubUrl":"https://github.com/grafana/k6/blob/3fcf5388d78cb382f0c0d42ec556a06bfd1b8dee/internal/js/modules/k6/browser/common/element_handle.go#L916-L952","documentation":"ElementHandle.focus() wraps the failure of a focus action that evaluates injected.focusNode on the element via CDP with no actionability preconditions. The wrapped error is usually a JavaScript evaluation failure (detached handle or destroyed execution context), a DOM error string returned by the injected script, or a timeout.","triggerScenarios":"The element handle was detached by navigation or an SPA re-render so Runtime.callFunctionOn fails; the page navigates during the call destroying the execution context; the element sits in a frame that was closed; the default timeout (30s) expires on a stalled session.","commonSituations":"Focusing fields in SPAs right after route changes; handles stored in variables across page.goto() calls; interacting with elements in popups/iframes that the page closed.","solutions":["Re-acquire the element handle right before focusing it","Wait for the page/frame to be ready: page.waitForLoadState() before the action","Verify the element is still attached: await el.evaluate(e => e.isConnected)","Raise K6_BROWSER_TIMEOUT if the session is just slow"],"exampleFix":"// before\nconst el = await page.$('#search');\nawait page.goto('https://app.example.com');\nawait el.focus(); // handle from previous page is dead\n\n// after\nawait page.goto('https://app.example.com');\nconst el = await page.$('#search');\nawait el.focus();","handlingStrategy":"try-catch","validationCode":"// Only focus elements confirmed to still be in the DOM\nconst connected = await el.evaluate((e) => e.isConnected).catch(() => false);\nif (!connected) throw new Error('handle is stale; re-query before focus()');\nawait el.focus();","typeGuard":null,"tryCatchPattern":"try {\n  await el.focus();\n} catch (e) {\n  if (/Could not find object|Execution context|timed out/i.test(String(e.message))) {\n    const fresh = await page.$(sel); // one re-query retry\n    await fresh.focus();\n  } else throw e;\n}","preventionTips":["Re-query elements after page.goto() or waitForNavigation()","Do not cache handles across async waits that can trigger re-renders","Raise K6_BROWSER_TIMEOUT for consistently slow environments instead of retrying blindly"],"tags":["browser","focus","dom","stale-element","execution-context","k6"],"backgroundTag":"element-action-timeout","analyzedSha":"3fcf5388d78cb382f0c0d42ec556a06bfd1b8dee","analyzedAt":"2026-08-18T03:05:52.393Z","contentChangedAt":"2026-08-18T03:05:52.393Z","schemaVersion":2},"datasetVersion":"2026-09-23T08:17:48.524Z"}