grafana/k6 · error

getting document element: %w

Error message

getting document element: %w

What it means

Returned by ElementHandle.ownerFrame() when the initial injected-script evaluation fails. OwnerFrame evaluates injected.getDocumentElement(node) with returnByValue:false to walk up to the document element; this error wraps an evaluation failure - typically the execution context being destroyed by an in-flight navigation, a stale/detached handle, or a closed CDP session.

Source

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

	}

	return ok, nil
}

// OwnerFrame returns the frame containing this element.
func (h *ElementHandle) OwnerFrame() (_ *Frame, rerr error) {
	fn := `
		(node, injected) => {
			return injected.getDocumentElement(node);
		}
	`
	opts := evalOptions{
		forceCallable: true,
		returnByValue: false,
	}
	res, err := h.evalWithScript(h.ctx, opts, fn)
	if err != nil {
		return nil, fmt.Errorf("getting document element: %w", err)
	}
	if res == nil {
		return nil, errors.New("getting document element: nil document")
	}

	documentHandle, ok := res.(*ElementHandle)
	if !ok {
		return nil, fmt.Errorf("unexpected result type while getting document element: %T", res)
	}
	defer func() {
		if err := documentHandle.Dispose(); err != nil {
			err = fmt.Errorf("disposing document element: %w", err)
			rerr = errors.Join(err, rerr)
		}
	}()

	if documentHandle.remoteObject.ObjectID == "" {
		return nil, err

View on GitHub (pinned to 93accf6570)

Solutions

  1. Wait for the page to settle first: await page.waitForLoadState('load')
  2. Re-query the handle immediately before ownerFrame()
  3. Retry the call once after load state if a navigation race is likely
  4. Ensure the page and browser are still open

Example fix

// before
const frame = await h.ownerFrame();
// after
await page.waitForLoadState('load');
const el = await page.$('#in-frame'); // fresh handle
const frame = await el.ownerFrame();
Defensive patterns

Strategy: retry

Validate before calling

await page.waitForLoadState('load');
const el = await page.$('#in-frame');
if (el) {
  const frame = await el.ownerFrame();
}

Try / catch

async function safeOwnerFrame(page, sel) {
  for (let i = 0; i < 2; i++) {
    try {
      const el = await page.$(sel);
      return await el.ownerFrame();
    } catch (e) {
      if (!String(e).includes('getting document element')) throw e;
      await page.waitForLoadState('load');
    }
  }
  throw new Error('ownerFrame failed after retry');
}

Prevention

When it happens

Trigger: Calling ownerFrame() while the page is navigating (context destroyed); on a handle detached by re-render; on a handle from a previous page; after the browser/page closed or the session dropped.

Common situations: Resolving the frame of an element right after a click that triggers navigation; iframes loading content concurrently; slow pages where navigation overlaps the call.

Related errors


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