grafana/k6 · error

evaluating handle for page: %w

Error message

evaluating handle for page: %w

What it means

Page.EvaluateHandle delegates to the main frame's EvaluateHandle and wraps any failure. The underlying error almost always comes from evaluating the expression in the page's main execution context: syntax errors, exceptions thrown by the evaluated function, or the context being destroyed by navigation/close while the call is in flight.

Source

Thrown at internal/js/modules/k6/browser/common/page.go:1126

	applySlowMo(p.ctx)

	return nil
}

// Evaluate runs JS code within the execution context of the main frame of the page.
func (p *Page) Evaluate(pageFunc string, args ...any) (any, error) {
	p.logger.Debugf("Page:Evaluate", "sid:%v", p.sessionID())

	return p.MainFrame().Evaluate(pageFunc, args...)
}

// EvaluateHandle runs JS code within the execution context of the main frame of the page.
func (p *Page) EvaluateHandle(pageFunc string, args ...any) (JSHandleAPI, error) {
	p.logger.Debugf("Page:EvaluateHandle", "sid:%v", p.sessionID())

	h, err := p.MainFrame().EvaluateHandle(pageFunc, args...)
	if err != nil {
		return nil, fmt.Errorf("evaluating handle for page: %w", err)
	}
	return h, nil
}

// Fill fills an input element with the provided value.
func (p *Page) Fill(selector string, value string, popts *FrameFillOptions) error {
	p.logger.Debugf("Page:Fill", "sid:%v selector:%s", p.sessionID(), selector)

	return p.MainFrame().Fill(selector, value, popts)
}

// Focus focuses an element matching the provided selector.
func (p *Page) Focus(selector string, popts *FrameBaseOptions) error {
	p.logger.Debugf("Page:Focus", "sid:%v selector:%s", p.sessionID(), selector)

	return p.MainFrame().Focus(selector, popts)
}

View on GitHub (pinned to 93accf6570)

Solutions

  1. Test the expression in DevTools console first to confirm it evaluates cleanly
  2. Wait for the needed state: await page.waitForSelector(...) or waitForLoadState before evaluating
  3. Wrap the evaluated function body in defensive null checks
  4. Ensure the page is not navigating or closing when the call runs

Example fix

// before
const h = await page.evaluateHandle('document.querySelector(\'#nope\').href'); // throws in page

// after
const h = await page.evaluateHandle("() => document.querySelector('#nope')?.href ?? null");
Defensive patterns

Strategy: try-catch

Validate before calling

if (page.isClosed()) throw new Error('page closed before evaluateHandle');

Try / catch

try {
  const h = await page.evaluateHandle(expr);
} catch (e) {
  if (/evaluating handle for page/.test(e.message)) { /* fix expr or await load, then retry */ }
  else throw e;
}

Prevention

When it happens

Trigger: page.evaluateHandle('document.body') with malformed JS, a function that throws (accessing a null property), or calling it during navigation so the execution context is destroyed and recreated.

Common situations: Passing a string that is not a valid expression; querying a selector before DOM ready so the function throws; evaluating during page.goto; page already closed.

Related errors


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