grafana/k6 · error

getting document element handle

Error message

getting document element handle: %w

What it means

Returned by Frame.newDocumentHandle when evaluating the expression 'document' in the frame's main world fails. The wrap adds call-site context to the evaluation error; typical causes are a missing or destroyed execution context (frame navigating) or a session error.

Solutions

  1. Retry once the frame's execution context exists (waitForExecutionContext runs first, but races remain)
  2. Ensure the frame is attached and not mid-navigation
  3. Inspect the wrapped error for the CDP cause
Defensive patterns

Strategy: retry

When it happens

Trigger: Thrown at internal/js/modules/k6/browser/common/frame.go:289 when the library encounters an invalid state.

Common situations: See trigger scenarios.


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

Appendix: source

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

	// see: nullContext().
	f.executionContextMu.RLock()
	defer f.executionContextMu.RUnlock()

	return f.documentHandle, f.documentHandle != nil
}

func (f *Frame) newDocumentHandle() (*ElementHandle, error) {
	result, err := f.evaluate(
		f.ctx,
		mainWorld,
		evalOptions{
			forceCallable: false,
			returnByValue: false,
		},
		"document",
	)
	if err != nil {
		return nil, fmt.Errorf("getting document element handle: %w", err)
	}
	if result == nil {
		return nil, fmt.Errorf("document element handle is nil")
	}
	dh, ok := result.(*ElementHandle)
	if !ok {
		return nil, fmt.Errorf("unexpected document handle type: %T", result)
	}

	return dh, nil
}

func (f *Frame) hasContext(world executionWorld) bool {
	f.executionContextMu.RLock()
	defer f.executionContextMu.RUnlock()

	return f.executionContexts[world] != nil
}

View on GitHub (pinned to 3fcf5388d7)