{"record":{"id":"d2c688106843db4b","repo":"grafana/k6","slug":"waiting-for-element-state-q-w","errorCode":null,"errorMessage":"waiting for element state %q: %w","messagePattern":"waiting for element state %q: %w","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"internal/js/modules/k6/browser/common/element_handle.go","lineNumber":1576,"sourceCode":"\t\treturn nil, handle.typ(apiCtx, text, KeyboardOptions{})\n\t}\n\ttypeAction := h.newAction(\n\t\t[]string{}, typ, false, withRetry, opts.NoWaitAfter, opts.Timeout,\n\t)\n\tif _, err := call(h.ctx, typeAction, opts.Timeout); err != nil {\n\t\treturn fmt.Errorf(\"typing text %q: %w\", text, err)\n\t}\n\n\tapplySlowMo(h.ctx)\n\n\treturn nil\n}\n\n// WaitForElementState waits for the element to reach the given state.\nfunc (h *ElementHandle) WaitForElementState(state string, opts *ElementHandleWaitForElementStateOptions) error {\n\t_, err := h.waitForElementState(h.ctx, []string{state}, opts.Timeout)\n\tif err != nil {\n\t\treturn fmt.Errorf(\"waiting for element state %q: %w\", state, err)\n\t}\n\n\treturn nil\n}\n\n// WaitForSelector waits for the selector to appear in the DOM.\nfunc (h *ElementHandle) WaitForSelector(selector string, opts *FrameWaitForSelectorOptions) (*ElementHandle, error) {\n\thandle, err := h.waitForSelector(h.ctx, selector, opts)\n\tif err != nil {\n\t\treturn nil, fmt.Errorf(\"waiting for selector %q: %w\", selector, err)\n\t}\n\n\treturn handle, nil\n}\n\n// evalWithScript evaluates the given js code in the scope of this ElementHandle and returns the result.\n// The js code can call helper functions from injected_script.js.\nfunc (h *ElementHandle) evalWithScript(","sourceCodeStart":1558,"sourceCodeEnd":1594,"githubUrl":"https://github.com/grafana/k6/blob/3fcf5388d78cb382f0c0d42ec556a06bfd1b8dee/internal/js/modules/k6/browser/common/element_handle.go#L1558-L1594","documentation":"Thrown by the k6 browser module's ElementHandle.waitForElementState(). It wraps failure of the internal waitForElementState call: the element did not reach the requested state before the timeout, the state check evaluation failed, or an invalid/unrecognized state string produced an error from the injected script.","triggerScenarios":"await el.waitForElementState('visible' | 'stable' | 'enabled', { timeout }) where the element never becomes that state — e.g., waiting 'enabled' on a button kept disabled by the app, or 'visible' on content behind an unexpanded tab; passing an unsupported state string; element detached during the wait.","commonSituations":"Waiting on a disabled submit button that only enables after form validation passes; accordion/tab content never shown; SPAs detaching the node during the wait; timeouts set lower than the app's real async delay (skeleton screens, spinners).","solutions":["Verify the state can actually occur: check the app enables/shows the element after the expected user step — fix the script order if not.","Increase the timeout to cover the app's real async behavior: el.waitForElementState('enabled', { timeout: '60s' }).","Wait on state 'attached' if visibility does not matter for your next action.","Re-acquire the handle if the page rerendered: stale handles fail all state waits."],"exampleFix":"// before\nconst btn = await page.$('#submit');\nawait btn.waitForElementState('enabled', { timeout: '5s' }); // validation slower than 5s\n\n// after\nconst btn = await page.waitForSelector('#submit');\nawait btn.waitForElementState('enabled', { timeout: '30s' });\nawait btn.click();","handlingStrategy":"retry","validationCode":"const state = await el.state().catch(() => null); // 'visible'|'hidden'|'enabled'|'disabled'|'stable'\nif (state === 'disabled') {\n  throw new Error('button still disabled; complete the prerequisite step first');\n}","typeGuard":null,"tryCatchPattern":"try {\n  await el.waitForElementState('enabled', { timeout: '30s' });\n} catch (e) {\n  if (String(e).includes('timed out')) {\n    await page.screenshot({ path: 'state-timeout.png' }); // diagnose actual page state\n    throw e;\n  }\n  throw e;\n}","preventionTips":["Match the waited state to the app's real gating (validation, terms) instead of guessing timeouts.","Set timeouts per wait based on measured app async delays, not the default."],"tags":["k6","browser","element-handle","wait","timeout"],"backgroundTag":"browser-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"}