grafana/k6 · error

waiting for selector %q: execution context %q not found

Error message

waiting for selector %q: execution context %q not found

What it means

waitForSelector found the element in the utility world and wants to adopt it into the main world, but the frame's main-world execution context is missing (nil). Contexts are destroyed and recreated on navigation; if the page navigated between finding the element and the adoption step, the mainWorld context is gone and this error names the selector and the missing world.

Source

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

	if handle == nil {
		return nil, fmt.Errorf("waiting for selector %q did not result in any nodes", selector)
	}

	// We always return ElementHandles in the main execution context (aka "DOM world")
	f.executionContextMu.RLock()
	defer f.executionContextMu.RUnlock()

	uec := f.executionContexts[utilityWorld]

	// An element should belong to the current main world execution context, and
	// not to the utility world context, otherwise, we should adopt it to the
	// current world's execution context. This is only valid when the handle
	// is from the current frame and not part of a nested frame.
	adopted := handle
	if uec != nil && uec == handle.execCtx {
		wec := f.executionContexts[mainWorld]
		if wec == nil {
			return nil, fmt.Errorf("waiting for selector %q: execution context %q not found", selector, mainWorld)
		}

		if adopted, err = wec.adoptElementHandle(handle); err != nil {
			return nil, fmt.Errorf("waiting for selector %q: adopting element handle: %w", selector, err)
		}

		if err = handle.Dispose(); err != nil {
			f.log.Warnf(
				"Frame:waitForSelector",
				"fid:%s furl:%q sel:%q disposing element handle: %v",
				f.ID(), f.URL(), selector, err,
			)
		}
	}

	return adopted, nil
}

View on GitHub (pinned to 93accf6570)

Solutions

  1. Retry the wait — the element will be found directly in the new context
  2. Wait for load state before querying
  3. Sequence queries after waitForNavigation to avoid the race window
Defensive patterns

Strategy: retry

Try / catch

try {
  el = await page.waitForSelector('#x');
} catch (e) {
  if (/execution context .* not found/.test(e.message)) {
    await page.waitForLoadState();
    el = await page.waitForSelector('#x');
  } else throw e;
}

Prevention

When it happens

Trigger: A navigation destroying the main-world context right after the element was found (inside waitForSelector's adoption step); the frame being detached at that moment; context creation lag on brand-new frames.

Common situations: Querying elements right as SPAs navigate; frames created and immediately navigated away; heavy pages where context (re)creation lags behind queries.

Related errors


AI-assisted analysis of grafana/k6@93accf6570 (2026-08-15). Data as JSON: /api/errors/3df071fdbc719321. Report an issue: GitHub.