grafana/k6 · error

getting element position: %w

Error message

getting element position: %w

What it means

Pointer actions with an explicit opts.Position call offsetPosition to compute the click point. It evaluates getElementBorderWidth in the page, converts the result, and takes the bounding box; failure means that evaluation/conversion failed, the bounding box could not be fetched, or the element was deemed not visible (nil box or zero border → ErrElementNotVisible).

Source

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

			}
			err = h.scrollRectIntoViewIfNeeded(apiCtx, rect)
		} else {
			_, err = h.eval(
				apiCtx,
				evalOptions{forceCallable: true, returnByValue: false},
				js.ScrollIntoView,
				sopts,
			)
		}
		if err != nil {
			return nil, fmt.Errorf("scrolling element into view: %w", err)
		}

		// Get the clickable point
		if p != nil {
			p, err = h.offsetPosition(apiCtx, opts.Position)
			if err != nil {
				return nil, fmt.Errorf("getting element position: %w", err)
			}
		} else {
			p, err = h.clickablePoint()
			if err != nil {
				return nil, fmt.Errorf("pointer action: %w", err)
			}
		}

		// Further translation of the point might be necessary if the point
		// is still relative to the parent frame it is in and not the page.
		*p, err = h.translatePointToPage(apiCtx, *p)
		if err != nil {
			return nil, fmt.Errorf("translating point to page: %w", err)
		}

		// Do a final actionability check to see if element can receive events
		// at mouse position in question
		if !opts.Force {

View on GitHub (pinned to 93accf6570)

Solutions

  1. Ensure the element is visible and laid out before using position: waitForSelector(sel, { state: 'visible' })
  2. Validate the box first: const b = el.boundingBox(); only click at position if b is non-null
  3. Omit position to use the element center, which tolerates border quirks better
  4. If coordinates are page-absolute, click with page.mouse.click(x, y) instead of an element-relative position

Example fix

// before
el.click({ position: { x: 5, y: 5 } }); // element hidden -> not visible

// after
const b = el.boundingBox();
if (b) {
  el.click({ position: { x: 5, y: 5 } });
}
Defensive patterns

Strategy: validation

Validate before calling

const b = el.boundingBox();
if (!b || b.width === 0 || b.height === 0) {
  throw new Error('element has no box; cannot click at a position');
}
el.click({ position: { x: 5, y: 5 } });

Try / catch

try {
  el.click({ position: { x: 5, y: 5 } });
} catch (e) {
  if (String(e).includes('getting element position')) {
    el.click(); // fall back to the element center
  } else {
    throw e;
  }
}

Prevention

When it happens

Trigger: Passing position: { x, y } to click/dblclick/hover/tap on an element that is hidden or has no layout (nil bounding box); border-width evaluation failing because the context was destroyed; offsets relative to a padding box the page keeps re-styling.

Common situations: Clicking precise coordinates inside canvas/maps/chart widgets where the center is not the target; positions computed from fixed assumptions that break at different viewport sizes; clicking custom-drawn controls with 0 border width combined with hidden containers.

Related errors


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