elements / key presses
Advanced element
Key Presses & Keyboard Events
Practicing non-printable key combinations, keyboard navigation, shortcuts, and asserting key event capture in automation frameworks.
Keyboard Event Detector
#target-key-input
Last Key Pressed:
NONE
Key code: —
- input
- #target-key-input
- output
- #key-result
- expected
- Pressing any keyboard key captures and displays the standardized key identifier (e.g. 'ENTER', 'ESCAPE', 'TAB', 'SPACE').
Code snippets
Python (Selenium)
from selenium.webdriver.common.keys import Keys
inp = driver.find_element(By.ID, "target-key-input")
inp.click()
inp.send_keys(Keys.ENTER)
result = driver.find_element(By.ID, "key-result").text
assert "ENTER" in result
# Test Escape key
inp.send_keys(Keys.ESCAPE)
assert "ESCAPE" in driver.find_element(By.ID, "key-result").textTypeScript (Playwright)
await page.click('#target-key-input');
await page.keyboard.press('Enter');
expect(await page.textContent('#key-result')).toContain('ENTER');
await page.keyboard.press('Escape');
expect(await page.textContent('#key-result')).toContain('ESCAPE');