grafana/k6 · error

getting bounding box

Error message

getting bounding box: %w

What it means

BoundingBox()'s generic failure branch: the underlying h.boundingBox() call errored without the 'Could not compute box model' signature, so it is not classified as invisibility. Causes are infrastructure-level: CDP GetBoxModel protocol errors, 'Could not compute box model' variants phrased differently, session/target closed, node detached, or frame position lookup failure.

Solutions

  1. Re-acquire the element handle right before the measurement instead of caching it across navigations
  2. Check page/browser liveness around the call (page.isClosed(), browser context state) and restart the iteration on session errors
  3. Retry once — transient CDP errors after load events often clear
  4. If it correlates with heavy pages in CI, raise browser memory or reduce parallelism

Example fix

// before
const el = await page.$('#target');
await page.goto(nextUrl);
const box = await el.boundingBox(); // stale handle

// after
await page.goto(nextUrl);
const el = await page.$('#target');
const box = await el.boundingBox();
Defensive patterns

Strategy: retry

Validate before calling

if (page.isClosed()) throw new Error('page closed; re-create before measuring');
const el = await page.$('#target'); // fresh handle right before use

Try / catch

let box;
for (let attempt = 0; attempt < 2; attempt++) {
  try {
    box = await el.boundingBox();
    break;
  } catch (e) {
    if (attempt === 1 || page.isClosed()) throw e;
    await page.waitForTimeout(500);
    el = await page.$('#target'); // re-acquire handle
  }
}

Prevention

When it happens

Trigger: Measuring or clicking an element after the page has navigated/crashed (stale remote object ID), the CDP session being closed mid-call, cross-origin frame position failures in h.frame.position(), or the DOM node being pruned between resolution and measurement.

Common situations: Long scripts holding element handles across navigations; browser or renderer crash under memory pressure in CI; targets swapped by SPA frameworks; flaky network to a remote Chrome (k6 cloud browsers).

Related errors


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

Appendix: source

Thrown at internal/js/modules/k6/browser/common/element_handle.go:828

		}
	}

	return strings.Join(parts, " >> ")
}

// AsElement returns this element handle.
func (h *ElementHandle) AsElement() *ElementHandle {
	return h
}

// BoundingBox returns this element's bounding box.
func (h *ElementHandle) BoundingBox() (*Rect, error) {
	bbox, err := h.boundingBox()
	if err != nil && strings.Contains(err.Error(), "Could not compute box model") {
		return nil, fmt.Errorf("%w: %w", ErrElementNotVisible, err)
	}
	if err != nil {
		return nil, fmt.Errorf("getting bounding box: %w", err)
	}
	return bbox, nil
}

// Click scrolls element into view and clicks in the center of the element
// TODO: look into making more robust using retries
// (see: https://github.com/microsoft/playwright/blob/master/src/server/dom.ts#L298)
func (h *ElementHandle) Click(opts *ElementHandleClickOptions) error {
	click := h.newPointerAction(
		func(apiCtx context.Context, handle *ElementHandle, p *Position) (any, error) {
			return nil, handle.click(p, opts.ToMouseClickOptions())
		},
		&opts.ElementHandleBasePointerOptions,
	)
	if _, err := call(h.ctx, click, opts.Timeout); err != nil {
		return fmt.Errorf("clicking on element: %w", err)
	}
	applySlowMo(h.ctx)

View on GitHub (pinned to 3fcf5388d7)