{"record":{"id":"9cc036648f47fb1b","repo":"grafana/k6","slug":"checking-element-is-checked-w","errorCode":null,"errorMessage":"checking element is checked: %w","messagePattern":"checking element is checked: %w","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"internal/js/modules/k6/browser/common/element_handle.go","lineNumber":1058,"sourceCode":"\tv, err := call(h.ctx, inputValueAction, opts.Timeout)\n\tif err != nil {\n\t\treturn \"\", fmt.Errorf(\"getting element's input value: %w\", err)\n\t}\n\n\ts, ok := v.(string)\n\tif !ok {\n\t\treturn \"\", fmt.Errorf(\"unexpected type %T (expecting string)\", v)\n\t}\n\n\treturn s, nil\n}\n\n// IsChecked checks if a checkbox or radio is checked.\nfunc (h *ElementHandle) IsChecked() (bool, error) {\n\tok, err := h.isChecked(h.ctx, 0)\n\t// We don't care about timeout errors here!\n\tif err != nil && !errors.Is(err, ErrTimedOut) {\n\t\treturn false, fmt.Errorf(\"checking element is checked: %w\", err)\n\t}\n\n\treturn ok, nil\n}\n\n// IsDisabled checks if the element is disabled.\nfunc (h *ElementHandle) IsDisabled() (bool, error) {\n\tok, err := h.isDisabled(h.ctx, 0)\n\t// We don't care anout timeout errors here!\n\tif err != nil && !errors.Is(err, ErrTimedOut) {\n\t\treturn false, fmt.Errorf(\"checking element is disabled: %w\", err)\n\t}\n\n\treturn ok, nil\n}\n\n// IsEditable checks if the element is editable.\nfunc (h *ElementHandle) IsEditable() (bool, error) {","sourceCodeStart":1040,"sourceCodeEnd":1076,"githubUrl":"https://github.com/grafana/k6/blob/3fcf5388d78cb382f0c0d42ec556a06bfd1b8dee/internal/js/modules/k6/browser/common/element_handle.go#L1040-L1076","documentation":"ElementHandle.isChecked() runs waitForElementState(['checked']) with a zero timeout and deliberately ignores ErrTimedOut ('We don't care about timeout errors here!'). Therefore this wrapped error is a hard failure from the injected script or CDP: typically the element handle is detached ('element is not attached to the DOM'), the execution context was destroyed by navigation, or the session is closed.","triggerScenarios":"Checking a checkbox handle after the SPA re-rendered and replaced the node; the page navigated between query and check; the element lives in a frame that was closed; the browser/page shut down before the call.","commonSituations":"React/Vue re-renders swapping checkbox nodes; route changes after caching a handle; logout or popup close mid-iteration; k6 iteration teardown racing the check.","solutions":["Re-query the element right before checking it, or use a live locator: await page.locator(sel).isChecked()","Wrap in try/catch and treat an error as 'element gone' rather than a hard failure when appropriate","Guard handle freshness: await el.evaluate(e => e.isConnected)","Avoid caching element handles across navigations or async waits"],"exampleFix":"// before\nconst el = await page.$('#agree');\nawait page.reload();\nconst checked = await el.isChecked(); // throws: stale handle\n\n// after\nawait page.reload();\nconst checked = await page.locator('#agree').isChecked();","handlingStrategy":"try-catch","validationCode":"// Use live locators for state checks; null-check for existence\nconst el = await page.$('#agree');\nif (el === null) throw new Error('checkbox no longer in DOM');\nconst checked = await page.locator('#agree').isChecked();","typeGuard":null,"tryCatchPattern":"let checked = false;\ntry {\n  checked = await el.isChecked();\n} catch (e) {\n  if (/not attached|Execution context|Could not find object/i.test(String(e.message))) {\n    checked = await page.locator(sel).isChecked(); // one live re-resolve\n  } else throw e;\n}","preventionTips":["Prefer page.locator(sel).isChecked() over cached handle checks","Re-acquire handles after re-renders that swap checkbox nodes","Remember: isChecked() never fails merely because the box is unchecked"],"tags":["browser","checkbox","element-state","stale-element","k6"],"backgroundTag":"stale-element-reference","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"}