grafana/k6 · error

waitFor retry threshold reached

Error message

waitFor retry threshold reached

What it means

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.

Solutions

  1. Settle navigation before waiting: await page.waitForLoadState('load') (or 'networkidle') or waitForNavigation, then waitForSelector
  2. Fix the WuT-side redirect/refresh loop — the retry budget cannot outgrow a page that never stabilizes
  3. Retry the waitForSelector call from the script after re-fetching the frame
  4. Increase the selector timeout option so inner polling has room, and upgrade k6 in case retry handling improved

Example fix

// before
await page.waitForSelector('.dashboard');

// after
await page.waitForLoadState('load');
await page.waitForSelector('.dashboard');
Defensive patterns

Strategy: retry

Validate before calling

// Remove the churn source before waiting
await page.waitForNavigation(); // or waitForLoadState('networkidle')
await page.waitForSelector('.ready', { timeout: '30s' });

Try / catch

async function waitForStable(page, sel, tries = 3) {
  for (let i = 0; i < tries; i++) {
    try { return await page.waitForSelector(sel); }
    catch (e) {
      if (!/retry threshold/.test(e.message)) throw e;
      await page.waitForLoadState('load');
    }
  }
  throw new Error(`selector ${sel} never stabilized`);
}

Prevention

When it happens

Trigger: 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.

Common situations: 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.

Related errors


AI-assisted analysis of grafana/k6@3fcf5388d7 (2026-08-18). Data as JSON: /api/errors/5202101ccc05fedf. Report an issue: GitHub.

Appendix: source

Thrown at internal/js/modules/k6/browser/common/frame.go:550

			f.log.Warnf(
				"Frame:waitForSelector",
				"fid:%s furl:%q sel:%q disposing element handle: %v",
				f.ID(), f.URL(), selector, err,
			)
		}
	}

	return adopted, nil
}

func (f *Frame) waitFor(
	selector string, opts *FrameWaitForSelectorOptions, retryCount int,
) (_ *ElementHandle, rerr error) {
	f.log.Debugf("Frame:waitFor", "fid:%s furl:%q sel:%q", f.ID(), f.URL(), selector)

	retryCount--
	if retryCount < 0 {
		return nil, errors.New("waitFor retry threshold reached")
	}

	document, err := f.document()
	if err != nil {
		if strings.Contains(err.Error(), "Cannot find context with specified id") {
			return f.waitFor(selector, opts, retryCount)
		}
		return nil, err
	}

	handle, err := document.waitForSelector(f.ctx, selector, opts)
	if err != nil {
		if strings.Contains(err.Error(), "Inspected target navigated or closed") {
			return f.waitFor(selector, opts, retryCount)
		}
		if strings.Contains(err.Error(), "Cannot find context with specified id") {
			return f.waitFor(selector, opts, retryCount)
		}

View on GitHub (pinned to 3fcf5388d7)