{"record":{"id":"8f7ce5fc6e52e43e","repo":"grafana/k6","slug":"pressing-q-on-q-w-8f7ce5","errorCode":null,"errorMessage":"pressing %q on %q: %w","messagePattern":"pressing %q on %q: %w","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"internal/js/modules/k6/browser/common/locator.go","lineNumber":548,"sourceCode":"\t\treturn nil, fmt.Errorf(\"selecting option on %q: %w\", l.selector, err)\n\t}\n\n\tapplySlowMo(l.ctx)\n\n\treturn v, nil\n}\n\n// Press the given key on the element found that matches the locator's\n// selector with strict mode on.\nfunc (l *Locator) Press(key string, opts *FramePressOptions) error {\n\tl.log.Debugf(\n\t\t\"Locator:Press\", \"fid:%s furl:%q sel:%q key:%q opts:%+v\",\n\t\tl.frame.ID(), l.frame.URL(), l.selector, key, opts,\n\t)\n\n\topts.Strict = true\n\tif err := l.frame.press(l.selector, key, opts); err != nil {\n\t\treturn fmt.Errorf(\"pressing %q on %q: %w\", key, l.selector, err)\n\t}\n\n\tapplySlowMo(l.ctx)\n\n\treturn nil\n}\n\n// PressSequentially focuses on the element and sequentially sends a keydown,\n// keypress, and keyup events for each character in the provided string.\n// For handling special keys, use the [Locator.Press] method.\nfunc (l *Locator) PressSequentially(text string, opts *FrameTypeOptions) error {\n\tl.log.Debugf(\n\t\t\"Locator:PressSequentially\", \"fid:%s furl:%q sel:%q text:%q opts:%+v\",\n\t\tl.frame.ID(), l.frame.URL(), l.selector, text, opts,\n\t)\n\t_, span := TraceAPICall(l.ctx, l.frame.page.targetID.String(), \"locator.pressSequentially\")\n\tdefer span.End()\n","sourceCodeStart":530,"sourceCodeEnd":566,"githubUrl":"https://github.com/grafana/k6/blob/93accf6570dcd306ca5e99cc44c393ee3797761b/internal/js/modules/k6/browser/common/locator.go#L530-L566","documentation":"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.","triggerScenarios":"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').","commonSituations":"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.","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"],"exampleFix":"// before\nawait page.locator('.search').press('Return'); // wrong key name\n\n// after\nawait page.locator('input.search').press('Enter');","handlingStrategy":"try-catch","validationCode":"const KEY_RE = /^(Enter|Tab|Escape|Arrow(Up|Down|Left|Right)|[a-zA-Z0-9]|Control\\+.|Shift\\+.|Alt\\+.|Meta\\+.)$/;\nif (!KEY_RE.test(key)) throw new Error(`invalid key: ${key}`);\nawait page.locator(sel).press(key);","typeGuard":"function isValidKey(k) {\n  return /^(Enter|Tab|Escape|Arrow(Up|Down|Left|Right)|Backspace|Delete|[a-zA-Z0-9]|((Control|Shift|Alt|Meta)\\+[a-zA-Z]))$/.test(k);\n}","tryCatchPattern":"try {\n  await page.locator(sel).press('Enter');\n} catch (e) {\n  if (/strict mode violation/i.test(e.message)) { /* narrow selector */ }\n  else if (/timeout/i.test(e.message)) { /* element not ready: wait and retry once */ }\n  else throw e;\n}","preventionTips":["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"],"tags":["browser","locator","keyboard","strict-mode"],"backgroundTag":null,"analyzedSha":"93accf6570dcd306ca5e99cc44c393ee3797761b","analyzedAt":"2026-08-15T21:23:27.118Z","schemaVersion":2},"datasetVersion":"2026-08-15T22:17:37.221Z"}