grafana/k6 · error

focusing %q: %w

Error message

focusing %q: %w

What it means

Frame.Focus() runs an action that waits for an element matching the selector to be attached and visible, then focuses it. This error wraps failures of that pipeline — typically 'waiting for selector ... timed out' or 'waiting for selector %q did not result in any nodes' when nothing matches, or DOM errors like element-not-connected.

Source

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

	}
	act := f.newAction(
		selector, DOMElementStateAttached, opts.Strict,
		fill, []string{"visible", "enabled", "editable"},
		opts.Force, withRetry, opts.NoWaitAfter, opts.Timeout,
	)
	if _, err := call(f.ctx, act, opts.Timeout); err != nil {
		return errorFromDOMError(err)
	}

	return nil
}

// Focus focuses on the first element that matches the selector.
func (f *Frame) Focus(selector string, popts *FrameBaseOptions) error {
	f.log.Debugf("Frame:Focus", "fid:%s furl:%q sel:%q", f.ID(), f.URL(), selector)

	if err := f.focus(selector, popts); err != nil {
		return fmt.Errorf("focusing %q: %w", selector, err)
	}

	applySlowMo(f.ctx)

	return nil
}

func (f *Frame) focus(selector string, opts *FrameBaseOptions) error {
	focus := func(apiCtx context.Context, handle *ElementHandle) (any, error) {
		return nil, handle.focus(apiCtx, true)
	}
	act := f.newAction(
		selector, DOMElementStateAttached, opts.Strict, focus,
		[]string{}, false, withRetry, true, opts.Timeout,
	)
	if _, err := call(f.ctx, act, opts.Timeout); err != nil {
		return errorFromDOMError(err)
	}

View on GitHub (pinned to 93accf6570)

Solutions

  1. Wait for visible state first: await frame.waitForSelector(sel, { state: 'visible' }).
  2. Use a unique selector and increase the timeout option.
  3. Target the correct frame for iframe-embedded fields.

Example fix

// before
frame.focus('#search', { timeout: 1000 });

// after
await frame.waitForSelector('#search', { state: 'visible', timeout: 30000 });
frame.focus('#search', { timeout: 30000 });
Defensive patterns

Strategy: validation

Validate before calling

await frame.waitForSelector(sel, { state: 'visible', timeout: 30000 });

Try / catch

try {
  frame.focus(sel, { timeout: 30000 });
} catch (e) {
  console.warn(`focus(${sel}) failed:`, e.message);
}

Prevention

When it happens

Trigger: Selector matches nothing/hidden element within opts.Timeout; strict-mode multiple matches; element detached between the wait and the focus call.

Common situations: Focusing fields rendered after async loads; focusing elements in the wrong frame; modals that steal focus while the action runs; shortened timeouts in CI.

Related errors


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