grafana/k6 · error
pressing %q on %q: %w
Error message
pressing %q on %q: %w
What it means
Thrown by Locator.Press() when the wrapped frame.press call fails. Strict mode is forced on, so the selector must resolve to exactly one element. Failures come from strict-mode violations, timeout while waiting for the element to be ready, the element not being focusable, or an unrecognized key name in the lower keyboard layer.
Source
Thrown at internal/js/modules/k6/browser/common/locator.go:548
return nil, fmt.Errorf("selecting option on %q: %w", l.selector, err)
}
applySlowMo(l.ctx)
return v, nil
}
// Press the given key on the element found that matches the locator's
// selector with strict mode on.
func (l *Locator) Press(key string, opts *FramePressOptions) error {
l.log.Debugf(
"Locator:Press", "fid:%s furl:%q sel:%q key:%q opts:%+v",
l.frame.ID(), l.frame.URL(), l.selector, key, opts,
)
opts.Strict = true
if err := l.frame.press(l.selector, key, opts); err != nil {
return fmt.Errorf("pressing %q on %q: %w", key, l.selector, err)
}
applySlowMo(l.ctx)
return nil
}
// PressSequentially focuses on the element and sequentially sends a keydown,
// keypress, and keyup events for each character in the provided string.
// For handling special keys, use the [Locator.Press] method.
func (l *Locator) PressSequentially(text string, opts *FrameTypeOptions) error {
l.log.Debugf(
"Locator:PressSequentially", "fid:%s furl:%q sel:%q text:%q opts:%+v",
l.frame.ID(), l.frame.URL(), l.selector, text, opts,
)
_, span := TraceAPICall(l.ctx, l.frame.page.targetID.String(), "locator.pressSequentially")
defer span.End()
View on GitHub (pinned to 93accf6570)
Solutions
- Use a unique selector that matches exactly one element
- Use valid key names: 'Enter', 'Tab', 'ArrowDown', or single characters with modifiers like 'Control+A'
- await locator.waitFor() before pressing if the element appears late
- Increase opts.timeout for slow pages
Example fix
// before
await page.locator('.search').press('Return'); // wrong key name
// after
await page.locator('input.search').press('Enter'); Defensive patterns
Strategy: try-catch
Validate before calling
const KEY_RE = /^(Enter|Tab|Escape|Arrow(Up|Down|Left|Right)|[a-zA-Z0-9]|Control\+.|Shift\+.|Alt\+.|Meta\+.)$/;
if (!KEY_RE.test(key)) throw new Error(`invalid key: ${key}`);
await page.locator(sel).press(key); Type guard
function isValidKey(k) {
return /^(Enter|Tab|Escape|Arrow(Up|Down|Left|Right)|Backspace|Delete|[a-zA-Z0-9]|((Control|Shift|Alt|Meta)\+[a-zA-Z]))$/.test(k);
} Try / catch
try {
await page.locator(sel).press('Enter');
} catch (e) {
if (/strict mode violation/i.test(e.message)) { /* narrow selector */ }
else if (/timeout/i.test(e.message)) { /* element not ready: wait and retry once */ }
else throw e;
} Prevention
- Use Playwright key names (Enter, ArrowDown), not X11 names (Return, Down)
- Wait for the input to be visible before pressing
- Keep a project constant list of the keys your scripts use
When it happens
Trigger: locator.press('key') where the selector matches 0 or >1 elements; the element is disabled/hidden past the timeout; the key string is not a valid Playwright-style key ('Enter', 'ArrowDown', 'a', 'Control+A').
Common situations: Pressing Enter on a search field that is rendered late; using a human key name like 'Return' or 'DOWN' instead of 'Enter'/'ArrowDown'; selector matching both the input and its wrapper.
Related errors
- getting input value of %q: %w
- selecting option on %q: %w
- hovering on %q: %w
- tapping on %q: %w
- dispatching locator event %q to %q: %w
AI-assisted analysis of grafana/k6@93accf6570 (2026-08-15).
Data as JSON: /api/errors/8f7ce5fc6e52e43e.
Report an issue: GitHub.