grafana/k6 · error

evaluating element

Error message

evaluating element: %w

What it means

Wrap of an ExecutionContext.Eval failure in BaseJSHandle.Evaluate: the provided page function is evaluated in the browser with the handle prepended as the first argument, and the wrapped error fires when evaluation itself fails — a JS exception thrown inside the page function, a syntax error in the function string, the execution context being destroyed by navigation, or the handle's object having been released.

Solutions

  1. Verify the page function compiles — test it in the browser devtools console
  2. Guard against navigation: wait for load/lifecycle events before evaluating
  3. Re-query the element handle if it may have been detached by DOM changes
  4. Wrap risky logic inside the page function in try/catch and return a sentinel value
Defensive patterns

Strategy: try-catch

When it happens

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

Common situations: See trigger scenarios.


AI-assisted analysis of grafana/k6@01ffac6f24 (2026-08-18). Data as JSON: /api/errors/4e0c6ad3c2ac8199. Report an issue: GitHub.

Appendix: source

Thrown at internal/js/modules/k6/browser/common/js_handle.go:140

	h.disposed = true
	if h.remoteObject.ObjectID == "" {
		return nil
	}
	act := runtime.ReleaseObject(h.remoteObject.ObjectID)
	if err := act.Do(cdp.WithExecutor(h.ctx, h.session)); err != nil {
		return fmt.Errorf("disposing element with ID %s: %w",
			h.remoteObject.ObjectID, err)
	}

	return nil
}

// Evaluate will evaluate provided page function within an execution context.
func (h *BaseJSHandle) Evaluate(pageFunc string, args ...any) (any, error) {
	args = append([]any{h}, args...)
	res, err := h.execCtx.Eval(h.ctx, pageFunc, args...)
	if err != nil {
		return nil, fmt.Errorf("evaluating element: %w", err)
	}

	return res, nil
}

// EvaluateHandle will evaluate provided page function within an execution context.
func (h *BaseJSHandle) EvaluateHandle(pageFunc string, args ...any) (JSHandleAPI, error) {
	args = append([]any{h}, args...)
	eh, err := h.execCtx.EvalHandle(h.ctx, pageFunc, args...)
	if err != nil {
		return nil, fmt.Errorf("evaluating handle for element: %w", err)
	}

	return eh, nil
}

// GetProperties retreives the JS handle's properties.
func (h *BaseJSHandle) GetProperties() (map[string]JSHandleAPI, error) {

View on GitHub (pinned to 01ffac6f24)