{"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/93accf6570dcd306ca5e99cc44c393ee3797761b/internal/js/modules/k6/browser/common/element_handle.go#L916-L952","documentation":"Returned by ElementHandle.focus() when focusing the element fails. Unlike fill/click, focus registers no actionability preconditions (empty checks list), so the wrapped cause is almost never a visibility failure: it is a stale handle (remote object released), a destroyed execution context from navigation, or an expired timeout while the internal action retried. The %w chain holds the underlying CDP error.","triggerScenarios":"handle.focus() on a handle obtained before a navigation or DOM re-render; the element was removed from the DOM; the execution context was destroyed mid-action by page.goto or a redirect; the page or browser was closed while focus was pending.","commonSituations":"Storing page.$() handles across await points in SPAs; focusing inside a loop after the page navigated between iterations; closing the browser context in an error branch while a focus is still in flight; slow pages where the default timeout is exceeded.","solutions":["Re-query the element handle immediately before focus() so it cannot be stale","Wait for the element first: page.waitForSelector(sel, { state: 'attached' })","Sequence navigations with page.waitForNavigation()/waitForLoadState() so focus runs in a stable context","Raise the timeout via the browser 'timeout' option if the app is slow to settle"],"exampleFix":"// before\nconst h = await page.$('#search');\nawait page.goto('https://example.com/next');\nawait h.focus(); // stale handle\n// after\nawait page.goto('https://example.com/next');\nawait page.waitForSelector('#search');\nconst h = await page.$('#search');\nawait h.focus();","handlingStrategy":"try-catch","validationCode":"await page.waitForSelector('#search', { state: 'attached' });\nconst h = await page.$('#search');\nif (await h.isVisible()) {\n  await h.focus();\n}","typeGuard":null,"tryCatchPattern":"try {\n  await handle.focus();\n} catch (e) {\n  if (String(e).includes('focusing on element')) {\n    const fresh = await page.$(sel);\n    if (fresh) await fresh.focus();\n  } else {\n    throw e;\n  }\n}","preventionTips":["Re-query handles after every navigation instead of reusing them","Use waitForNavigation/waitForLoadState to sequence focus after redirects","Keep handles scoped to a single synchronous block of interaction"],"tags":["browser","element-handle","focus","detached-handle","timeout"],"backgroundTag":null,"analyzedSha":"93accf6570dcd306ca5e99cc44c393ee3797761b","analyzedAt":"2026-08-15T21:23:27.118Z","schemaVersion":2},"datasetVersion":"2026-08-15T22:17:37.221Z"}