{"record":{"id":"5202101ccc05fedf","repo":"grafana/k6","slug":"waitfor-retry-threshold-reached","errorCode":null,"errorMessage":"waitFor retry threshold reached","messagePattern":"waitFor retry threshold reached","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"internal/js/modules/k6/browser/common/frame.go","lineNumber":550,"sourceCode":"\t\t\tf.log.Warnf(\n\t\t\t\t\"Frame:waitForSelector\",\n\t\t\t\t\"fid:%s furl:%q sel:%q disposing element handle: %v\",\n\t\t\t\tf.ID(), f.URL(), selector, err,\n\t\t\t)\n\t\t}\n\t}\n\n\treturn adopted, nil\n}\n\nfunc (f *Frame) waitFor(\n\tselector string, opts *FrameWaitForSelectorOptions, retryCount int,\n) (_ *ElementHandle, rerr error) {\n\tf.log.Debugf(\"Frame:waitFor\", \"fid:%s furl:%q sel:%q\", f.ID(), f.URL(), selector)\n\n\tretryCount--\n\tif retryCount < 0 {\n\t\treturn nil, errors.New(\"waitFor retry threshold reached\")\n\t}\n\n\tdocument, err := f.document()\n\tif err != nil {\n\t\tif strings.Contains(err.Error(), \"Cannot find context with specified id\") {\n\t\t\treturn f.waitFor(selector, opts, retryCount)\n\t\t}\n\t\treturn nil, err\n\t}\n\n\thandle, err := document.waitForSelector(f.ctx, selector, opts)\n\tif err != nil {\n\t\tif strings.Contains(err.Error(), \"Inspected target navigated or closed\") {\n\t\t\treturn f.waitFor(selector, opts, retryCount)\n\t\t}\n\t\tif strings.Contains(err.Error(), \"Cannot find context with specified id\") {\n\t\t\treturn f.waitFor(selector, opts, retryCount)\n\t\t}","sourceCodeStart":532,"sourceCodeEnd":568,"githubUrl":"https://github.com/grafana/k6/blob/3fcf5388d78cb382f0c0d42ec556a06bfd1b8dee/internal/js/modules/k6/browser/common/frame.go#L532-L568","documentation":"Frame.waitForSelector delegates to waitFor (frame.go:535-570) with a hard-coded budget of 20 attempts (frame.go:494). waitFor retries on transient navigation-era errors — 'Inspected target navigated or closed', 'Cannot find context with specified id', 'Execution context was destroyed', and visibility errors — decrementing the count each time. When all 20 attempts are consumed, this error surfaces.","triggerScenarios":"Waiting for a selector while the page navigates continuously (redirect chains, meta refresh, SPA re-render loops); an element flapping between visible/hidden states; execution contexts being destroyed and recreated faster than 20 retries can absorb.","commonSituations":"Login flows with several redirects; ad-heavy or auto-refreshing pages under load; slow CI machines where context churn during load is high; iframes reloaded in a loop.","solutions":["Settle navigation before waiting: await page.waitForLoadState('load') (or 'networkidle') or waitForNavigation, then waitForSelector","Fix the WuT-side redirect/refresh loop — the retry budget cannot outgrow a page that never stabilizes","Retry the waitForSelector call from the script after re-fetching the frame","Increase the selector timeout option so inner polling has room, and upgrade k6 in case retry handling improved"],"exampleFix":"// before\nawait page.waitForSelector('.dashboard');\n\n// after\nawait page.waitForLoadState('load');\nawait page.waitForSelector('.dashboard');","handlingStrategy":"retry","validationCode":"// Remove the churn source before waiting\nawait page.waitForNavigation(); // or waitForLoadState('networkidle')\nawait page.waitForSelector('.ready', { timeout: '30s' });","typeGuard":null,"tryCatchPattern":"async function waitForStable(page, sel, tries = 3) {\n  for (let i = 0; i < tries; i++) {\n    try { return await page.waitForSelector(sel); }\n    catch (e) {\n      if (!/retry threshold/.test(e.message)) throw e;\n      await page.waitForLoadState('load');\n    }\n  }\n  throw new Error(`selector ${sel} never stabilized`);\n}","preventionTips":["Wait for load/networkidle before waitForSelector on redirect-heavy flows","Fix meta-refresh/redirect loops in the WuT — retries cannot outpace them","Avoid waiting for selectors during known transition windows","Keep the k6 version current; retry internals have improved across releases"],"tags":["browser","waitforselector","retry","navigation"],"backgroundTag":"wait-for-selector-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"}