Playwright Keys and Shortcuts

Profile picture for user arilio666

In playwright with the method locator.press, we can simulate any keyboard events using this magnificent feature. This method focuses on the single element provided and produces a single keystroke.

The press method accepts the logical key name we usually use, which is emitted into the keyboard.key property of the keyboard events.

These are some of the keyboard logical names.

Backquote, Minus, Equal, Backslash, Backspace, Tab, Delete, Escape,
ArrowDown, End, Enter, Home, Insert, PageDown, PageUp, ArrowRight,
ArrowUp, F1 - F12, Digit0 - Digit9, KeyA - KeyZ, etc.
await page.goto("https://www.programsbuzz.com/");

await page.locator('.fas.fa-search').click();
await page.locator('#edit-keys').type('Cypress')

await page.locator('#edit-keys').press('Control+KeyA+Backspace')
await page.locator('#edit-keys').type('Playwright')
await page.locator('#edit-keys').press('Control+KeyA+Backspace')
await page.locator('#edit-keys').press('Shift+A+P+P+L+E')

await page.locator('#edit-keys').press('Enter')
  • Here is a simple example of keypress events using the control key, key A, backspace, and enter.
  • A combination of keys is allowed, just like keyboard shortcuts.
  • Shift, alt, and meta are also supported.

Conclusion:

Likewise, key shortcuts can be played around using this pressing method by playwright to make things smooth and more manageable.