grafana/k6 · error

getting browser window ID: %w

Error message

getting browser window ID: %w

What it means

For frame sessions with a UI window (main frames, not iframes), NewFrameSession calls Browser.getWindowForTarget (frame_session.go:155) to record the window ID. If that CDP command fails, session creation aborts with this wrapped error. It indicates the target/window no longer exists from chromium's perspective, or the CDP session is broken.

Source

Thrown at internal/js/modules/k6/browser/common/frame_session.go:155

	if err != nil {
		l.Debugf("NewFrameSession:NewNetworkManager", "sid:%v tid:%v err:%v",
			s.ID(), tid, err)
		return nil, err
	}

	// When a frame creates a new FrameSession without UI (e.g. some iframes) we cannot
	// retrieve the windowID. Doing so would lead to an error from chromium. For now all
	// iframes that are attached are setup with hasUIWindow as false which seems to work
	// as expected for iframes with and without UI elements.
	if fs.hasUIWindow {
		action := browser.GetWindowForTarget().WithTargetID(fs.targetID)
		if fs.windowID, _, err = action.Do(cdp.WithExecutor(fs.ctx, fs.session)); err != nil {
			l.Debugf(
				"NewFrameSession:GetWindowForTarget",
				"sid:%v tid:%v err:%v",
				s.ID(), tid, err)

			return nil, fmt.Errorf("getting browser window ID: %w", err)
		}
	}

	fs.initEvents()
	if err = fs.initFrameTree(); err != nil {
		l.Debugf(
			"NewFrameSession:initFrameTree",
			"sid:%v tid:%v err:%v",
			s.ID(), tid, err)

		return nil, err
	}
	if err = fs.initIsolatedWorld(utilityWorldName); err != nil {
		l.Debugf(
			"NewFrameSession:initIsolatedWorld",
			"sid:%v tid:%v err:%v",
			s.ID(), tid, err)

View on GitHub (pinned to 93accf6570)

Solutions

  1. Ensure pages are not being closed concurrently while they initialize; sequence page creation and close() in the same VU
  2. For headed mode in CI, provide a display (xvfb-run) or use K6_BROWSER_HEADLESS=true
  3. If it happens randomly under load, suspect chromium stability/resources (memory limits, zombie processes) and restart the browser between iterations
Defensive patterns

Strategy: retry

Try / catch

try {
  const page = browser.newPage();
} catch (e) {
  if (/getting browser window ID/i.test(String(e))) {
    // target/window vanished during init — close context and retry once
  }
}

Prevention

When it happens

Trigger: Creating or navigating a page whose window is destroyed concurrently (window.close(), browser shutdown), chromium crashing during page setup, or headed mode in an environment without a display/Wayland-X11 issues.

Common situations: Scripts that open and rapidly close pages under load, Xvfb/display misconfiguration for headed chromium in CI, and remote-browser setups where the target is closed between attach and this call.

Related errors


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