Keyboard Actions
Master keyboard interactions including special keys, key combinations, and the Actions API.
Selenium 3 & 4 Stable
Keyboard interactions go beyond simple typing. This guide covers special keys, key combinations, and advanced keyboard actions using the Actions API.
Special Keys
Selenium provides constants for special keys that can’t be typed directly:
Using Special Keys
Selenium 3 & 4 Stable
import org.openqa.selenium.Keys;import org.openqa.selenium.WebElement;
WebElement input = driver.findElement(By.id("search"));
// Navigation keysinput.sendKeys(Keys.ENTER); // Press Enterinput.sendKeys(Keys.TAB); // Move to next fieldinput.sendKeys(Keys.ESCAPE); // Press Escapeinput.sendKeys(Keys.BACK_SPACE); // Delete character before cursorinput.sendKeys(Keys.DELETE); // Delete character after cursor
// Arrow keysinput.sendKeys(Keys.ARROW_UP);input.sendKeys(Keys.ARROW_DOWN);input.sendKeys(Keys.ARROW_LEFT);input.sendKeys(Keys.ARROW_RIGHT);
// Home/End/Pageinput.sendKeys(Keys.HOME); // Go to beginninginput.sendKeys(Keys.END); // Go to endinput.sendKeys(Keys.PAGE_UP);input.sendKeys(Keys.PAGE_DOWN);
// Function keysinput.sendKeys(Keys.F1); // Help (in some apps)input.sendKeys(Keys.F5); // Refresh (browser)input.sendKeys(Keys.F12); // DevTools (browser)
// Combine with textinput.sendKeys("hello" + Keys.ENTER);from selenium.webdriver.common.keys import Keysfrom selenium.webdriver.common.by import By
input_field = driver.find_element(By.ID, "search")
# Navigation keysinput_field.send_keys(Keys.ENTER) # Press Enterinput_field.send_keys(Keys.TAB) # Move to next fieldinput_field.send_keys(Keys.ESCAPE) # Press Escapeinput_field.send_keys(Keys.BACK_SPACE) # Delete character before cursorinput_field.send_keys(Keys.DELETE) # Delete character after cursor
# Arrow keysinput_field.send_keys(Keys.ARROW_UP)input_field.send_keys(Keys.ARROW_DOWN)input_field.send_keys(Keys.ARROW_LEFT)input_field.send_keys(Keys.ARROW_RIGHT)
# Home/End/Pageinput_field.send_keys(Keys.HOME) # Go to beginninginput_field.send_keys(Keys.END) # Go to endinput_field.send_keys(Keys.PAGE_UP)input_field.send_keys(Keys.PAGE_DOWN)
# Function keysinput_field.send_keys(Keys.F1) # Help (in some apps)input_field.send_keys(Keys.F5) # Refresh (browser)input_field.send_keys(Keys.F12) # DevTools (browser)
# Combine with textinput_field.send_keys("hello" + Keys.ENTER)const { Key } = require('selenium-webdriver');
const input = await driver.findElement(By.id('search'));
// Navigation keysawait input.sendKeys(Key.ENTER); // Press Enterawait input.sendKeys(Key.TAB); // Move to next fieldawait input.sendKeys(Key.ESCAPE); // Press Escapeawait input.sendKeys(Key.BACK_SPACE); // Delete character before cursorawait input.sendKeys(Key.DELETE); // Delete character after cursor
// Arrow keysawait input.sendKeys(Key.ARROW_UP);await input.sendKeys(Key.ARROW_DOWN);await input.sendKeys(Key.ARROW_LEFT);await input.sendKeys(Key.ARROW_RIGHT);
// Home/End/Pageawait input.sendKeys(Key.HOME); // Go to beginningawait input.sendKeys(Key.END); // Go to endawait input.sendKeys(Key.PAGE_UP);await input.sendKeys(Key.PAGE_DOWN);
// Function keysawait input.sendKeys(Key.F1); // Help (in some apps)await input.sendKeys(Key.F5); // Refresh (browser)await input.sendKeys(Key.F12); // DevTools (browser)
// Combine with textawait input.sendKeys('hello', Key.ENTER);using OpenQA.Selenium;
IWebElement input = driver.FindElement(By.Id("search"));
// Navigation keysinput.SendKeys(Keys.Enter); // Press Enterinput.SendKeys(Keys.Tab); // Move to next fieldinput.SendKeys(Keys.Escape); // Press Escapeinput.SendKeys(Keys.Backspace); // Delete character before cursorinput.SendKeys(Keys.Delete); // Delete character after cursor
// Arrow keysinput.SendKeys(Keys.ArrowUp);input.SendKeys(Keys.ArrowDown);input.SendKeys(Keys.ArrowLeft);input.SendKeys(Keys.ArrowRight);
// Home/End/Pageinput.SendKeys(Keys.Home); // Go to beginninginput.SendKeys(Keys.End); // Go to endinput.SendKeys(Keys.PageUp);input.SendKeys(Keys.PageDown);
// Function keysinput.SendKeys(Keys.F1); // Help (in some apps)input.SendKeys(Keys.F5); // Refresh (browser)input.SendKeys(Keys.F12); // DevTools (browser)
// Combine with textinput.SendKeys("hello" + Keys.Enter);Key Combinations (Modifier Keys)
Use modifier keys (Ctrl, Alt, Shift) for keyboard shortcuts:
Key Combinations
Selenium 3 & 4 Stable
import org.openqa.selenium.Keys;
WebElement textArea = driver.findElement(By.id("editor"));
// Select all (Ctrl+A)textArea.sendKeys(Keys.chord(Keys.CONTROL, "a"));
// Copy (Ctrl+C)textArea.sendKeys(Keys.chord(Keys.CONTROL, "c"));
// Paste (Ctrl+V)textArea.sendKeys(Keys.chord(Keys.CONTROL, "v"));
// Cut (Ctrl+X)textArea.sendKeys(Keys.chord(Keys.CONTROL, "x"));
// Undo (Ctrl+Z)textArea.sendKeys(Keys.chord(Keys.CONTROL, "z"));
// Redo (Ctrl+Y or Ctrl+Shift+Z)textArea.sendKeys(Keys.chord(Keys.CONTROL, "y"));textArea.sendKeys(Keys.chord(Keys.CONTROL, Keys.SHIFT, "z"));
// Save (Ctrl+S) - useful for web editorstextArea.sendKeys(Keys.chord(Keys.CONTROL, "s"));
// Find (Ctrl+F)driver.findElement(By.tagName("body")).sendKeys(Keys.chord(Keys.CONTROL, "f"));
// New tab (Ctrl+T)driver.findElement(By.tagName("body")).sendKeys(Keys.chord(Keys.CONTROL, "t"));
// Mac users: Use Keys.COMMAND instead of Keys.CONTROLString osName = System.getProperty("os.name").toLowerCase();Keys modifier = osName.contains("mac") ? Keys.COMMAND : Keys.CONTROL;textArea.sendKeys(Keys.chord(modifier, "a"));import platform
text_area = driver.find_element(By.ID, "editor")
# Select all (Ctrl+A)text_area.send_keys(Keys.CONTROL + "a")
# Copy (Ctrl+C)text_area.send_keys(Keys.CONTROL + "c")
# Paste (Ctrl+V)text_area.send_keys(Keys.CONTROL + "v")
# Cut (Ctrl+X)text_area.send_keys(Keys.CONTROL + "x")
# Undo (Ctrl+Z)text_area.send_keys(Keys.CONTROL + "z")
# Redo (Ctrl+Y or Ctrl+Shift+Z)text_area.send_keys(Keys.CONTROL + "y")text_area.send_keys(Keys.CONTROL + Keys.SHIFT + "z")
# Save (Ctrl+S) - useful for web editorstext_area.send_keys(Keys.CONTROL + "s")
# Find (Ctrl+F)driver.find_element(By.TAG_NAME, "body").send_keys(Keys.CONTROL + "f")
# Mac users: Use Keys.COMMAND instead of Keys.CONTROLmodifier = Keys.COMMAND if platform.system() == "Darwin" else Keys.CONTROLtext_area.send_keys(modifier + "a")const input = await driver.findElement(By.id('editor'));
// Select all (Ctrl+A)await input.sendKeys(Key.chord(Key.CONTROL, 'a'));
// Copy (Ctrl+C)await input.sendKeys(Key.chord(Key.CONTROL, 'c'));
// Paste (Ctrl+V)await input.sendKeys(Key.chord(Key.CONTROL, 'v'));
// Cut (Ctrl+X)await input.sendKeys(Key.chord(Key.CONTROL, 'x'));
// Undo (Ctrl+Z)await input.sendKeys(Key.chord(Key.CONTROL, 'z'));
// Redo (Ctrl+Y)await input.sendKeys(Key.chord(Key.CONTROL, 'y'));
// Save (Ctrl+S)await input.sendKeys(Key.chord(Key.CONTROL, 's'));
// Find (Ctrl+F)const body = await driver.findElement(By.tagName('body'));await body.sendKeys(Key.chord(Key.CONTROL, 'f'));
// Platform-specific modifierconst os = require('os');const modifier = os.platform() === 'darwin' ? Key.COMMAND : Key.CONTROL;await input.sendKeys(Key.chord(modifier, 'a'));IWebElement textArea = driver.FindElement(By.Id("editor"));
// Select all (Ctrl+A)textArea.SendKeys(Keys.Control + "a");
// Copy (Ctrl+C)textArea.SendKeys(Keys.Control + "c");
// Paste (Ctrl+V)textArea.SendKeys(Keys.Control + "v");
// Cut (Ctrl+X)textArea.SendKeys(Keys.Control + "x");
// Undo (Ctrl+Z)textArea.SendKeys(Keys.Control + "z");
// Redo (Ctrl+Y or Ctrl+Shift+Z)textArea.SendKeys(Keys.Control + "y");textArea.SendKeys(Keys.Control + Keys.Shift + "z");
// Save (Ctrl+S)textArea.SendKeys(Keys.Control + "s");
// Find (Ctrl+F)driver.FindElement(By.TagName("body")).SendKeys(Keys.Control + "f");
// Platform-specific modifierbool isMac = RuntimeInformation.IsOSPlatform(OSPlatform.OSX);string modifier = isMac ? Keys.Command : Keys.Control;textArea.SendKeys(modifier + "a");Actions API for Keyboard
The Actions API provides more control over keyboard interactions:
Actions API Keyboard Control
Selenium 3 & 4 Stable
import org.openqa.selenium.interactions.Actions;
Actions actions = new Actions(driver);
// Hold down modifier key while typingactions.keyDown(Keys.SHIFT) .sendKeys("hello") // Types "HELLO" .keyUp(Keys.SHIFT) .perform();
// Select text with Shift+ArrowWebElement input = driver.findElement(By.id("text-field"));input.sendKeys("Hello World");input.sendKeys(Keys.HOME); // Go to start
actions.keyDown(Keys.SHIFT) .sendKeys(Keys.END) // Select all .keyUp(Keys.SHIFT) .perform();
// Multiple key pressesactions.keyDown(Keys.CONTROL) .keyDown(Keys.SHIFT) .sendKeys("n") // Ctrl+Shift+N (new incognito window in Chrome) .keyUp(Keys.SHIFT) .keyUp(Keys.CONTROL) .perform();
// Press and release keys without an elementactions.sendKeys(Keys.ESCAPE).perform();
// Send keys to specific elementactions.sendKeys(input, "typed text").perform();from selenium.webdriver.common.action_chains import ActionChains
actions = ActionChains(driver)
# Hold down modifier key while typingactions.key_down(Keys.SHIFT) .send_keys("hello") .key_up(Keys.SHIFT) .perform() # Types "HELLO"
# Select text with Shift+Arrowinput_field = driver.find_element(By.ID, "text-field")input_field.send_keys("Hello World")input_field.send_keys(Keys.HOME) # Go to start
actions = ActionChains(driver) # Reset chainactions.key_down(Keys.SHIFT) .send_keys(Keys.END) .key_up(Keys.SHIFT) .perform() # Select all
# Multiple key pressesactions = ActionChains(driver)actions.key_down(Keys.CONTROL) .key_down(Keys.SHIFT) .send_keys("n") .key_up(Keys.SHIFT) .key_up(Keys.CONTROL) .perform()
# Press and release keys without an elementactions = ActionChains(driver)actions.send_keys(Keys.ESCAPE).perform()
# Send keys to specific elementactions = ActionChains(driver)actions.send_keys_to_element(input_field, "typed text").perform()const actions = driver.actions({ async: true });
// Hold down modifier key while typingawait actions .keyDown(Key.SHIFT) .sendKeys('hello') // Types "HELLO" .keyUp(Key.SHIFT) .perform();
// Select text with Shift+Arrowconst input = await driver.findElement(By.id('text-field'));await input.sendKeys('Hello World');await input.sendKeys(Key.HOME); // Go to start
await driver.actions({ async: true }) .keyDown(Key.SHIFT) .sendKeys(Key.END) // Select all .keyUp(Key.SHIFT) .perform();
// Multiple key pressesawait driver.actions({ async: true }) .keyDown(Key.CONTROL) .keyDown(Key.SHIFT) .sendKeys('n') .keyUp(Key.SHIFT) .keyUp(Key.CONTROL) .perform();
// Press and release keys without an elementawait driver.actions({ async: true }) .sendKeys(Key.ESCAPE) .perform();
// Send keys to specific elementawait driver.actions({ async: true }) .sendKeys(input, 'typed text') .perform();using OpenQA.Selenium.Interactions;
Actions actions = new Actions(driver);
// Hold down modifier key while typingactions.KeyDown(Keys.Shift) .SendKeys("hello") // Types "HELLO" .KeyUp(Keys.Shift) .Perform();
// Select text with Shift+ArrowIWebElement input = driver.FindElement(By.Id("text-field"));input.SendKeys("Hello World");input.SendKeys(Keys.Home); // Go to start
new Actions(driver) .KeyDown(Keys.Shift) .SendKeys(Keys.End) // Select all .KeyUp(Keys.Shift) .Perform();
// Multiple key pressesnew Actions(driver) .KeyDown(Keys.Control) .KeyDown(Keys.Shift) .SendKeys("n") .KeyUp(Keys.Shift) .KeyUp(Keys.Control) .Perform();
// Press and release keys without an elementnew Actions(driver).SendKeys(Keys.Escape).Perform();
// Send keys to specific elementnew Actions(driver).SendKeys(input, "typed text").Perform();Common Keyboard Patterns
Tab Navigation
Tab Through Form Fields
Selenium 3 & 4 Stable
// Fill form using Tab to move between fieldsdriver.findElement(By.id("firstName")).sendKeys("John" + Keys.TAB);// Cursor now in next fielddriver.switchTo().activeElement().sendKeys("Doe" + Keys.TAB);// Continue...driver.switchTo().activeElement().sendKeys("john@example.com" + Keys.TAB);
// Shift+Tab to go backwardsdriver.switchTo().activeElement().sendKeys(Keys.chord(Keys.SHIFT, Keys.TAB));# Fill form using Tab to move between fieldsdriver.find_element(By.ID, "firstName").send_keys("John" + Keys.TAB)# Cursor now in next fielddriver.switch_to.active_element.send_keys("Doe" + Keys.TAB)# Continue...driver.switch_to.active_element.send_keys("john@example.com" + Keys.TAB)
# Shift+Tab to go backwardsdriver.switch_to.active_element.send_keys(Keys.SHIFT + Keys.TAB)// Fill form using Tab to move between fieldsawait driver.findElement(By.id('firstName')).sendKeys('John', Key.TAB);// Cursor now in next fieldawait driver.switchTo().activeElement().sendKeys('Doe', Key.TAB);// Continue...await driver.switchTo().activeElement().sendKeys('john@example.com', Key.TAB);
// Shift+Tab to go backwardsawait driver.switchTo().activeElement().sendKeys(Key.chord(Key.SHIFT, Key.TAB));// Fill form using Tab to move between fieldsdriver.FindElement(By.Id("firstName")).SendKeys("John" + Keys.Tab);// Cursor now in next fielddriver.SwitchTo().ActiveElement().SendKeys("Doe" + Keys.Tab);// Continue...driver.SwitchTo().ActiveElement().SendKeys("john@example.com" + Keys.Tab);
// Shift+Tab to go backwardsdriver.SwitchTo().ActiveElement().SendKeys(Keys.Shift + Keys.Tab);Text Selection and Editing
Text Selection Patterns
Selenium 3 & 4 Stable
WebElement editor = driver.findElement(By.id("editor"));editor.sendKeys("Hello World");
// Select word with double Ctrl+Shift+ArrowActions actions = new Actions(driver);actions.click(editor) .keyDown(Keys.CONTROL) .keyDown(Keys.SHIFT) .sendKeys(Keys.ARROW_LEFT) // Select previous word .keyUp(Keys.SHIFT) .keyUp(Keys.CONTROL) .perform();
// Delete selected textactions.sendKeys(Keys.DELETE).perform();
// Select all and replaceeditor.sendKeys(Keys.chord(Keys.CONTROL, "a"));editor.sendKeys("New content"); // Replaces alleditor = driver.find_element(By.ID, "editor")editor.send_keys("Hello World")
# Select word with double Ctrl+Shift+Arrowactions = ActionChains(driver)actions.click(editor) .key_down(Keys.CONTROL) .key_down(Keys.SHIFT) .send_keys(Keys.ARROW_LEFT) .key_up(Keys.SHIFT) .key_up(Keys.CONTROL) .perform()
# Delete selected textActionChains(driver).send_keys(Keys.DELETE).perform()
# Select all and replaceeditor.send_keys(Keys.CONTROL + "a")editor.send_keys("New content") # Replaces allconst editor = await driver.findElement(By.id('editor'));await editor.sendKeys('Hello World');
// Select word with Ctrl+Shift+Arrowawait driver.actions({ async: true }) .click(editor) .keyDown(Key.CONTROL) .keyDown(Key.SHIFT) .sendKeys(Key.ARROW_LEFT) // Select previous word .keyUp(Key.SHIFT) .keyUp(Key.CONTROL) .perform();
// Delete selected textawait driver.actions({ async: true }) .sendKeys(Key.DELETE) .perform();
// Select all and replaceawait editor.sendKeys(Key.chord(Key.CONTROL, 'a'));await editor.sendKeys('New content'); // Replaces allIWebElement editor = driver.FindElement(By.Id("editor"));editor.SendKeys("Hello World");
// Select word with Ctrl+Shift+Arrownew Actions(driver) .Click(editor) .KeyDown(Keys.Control) .KeyDown(Keys.Shift) .SendKeys(Keys.ArrowLeft) // Select previous word .KeyUp(Keys.Shift) .KeyUp(Keys.Control) .Perform();
// Delete selected textnew Actions(driver).SendKeys(Keys.Delete).Perform();
// Select all and replaceeditor.SendKeys(Keys.Control + "a");editor.SendKeys("New content"); // Replaces allKey Reference Table
| Action | Windows/Linux | Mac |
|---|---|---|
| Select All | Ctrl+A | Cmd+A |
| Copy | Ctrl+C | Cmd+C |
| Paste | Ctrl+V | Cmd+V |
| Cut | Ctrl+X | Cmd+X |
| Undo | Ctrl+Z | Cmd+Z |
| Redo | Ctrl+Y | Cmd+Shift+Z |
| Save | Ctrl+S | Cmd+S |
| Find | Ctrl+F | Cmd+F |
| New Tab | Ctrl+T | Cmd+T |
| Close Tab | Ctrl+W | Cmd+W |
Best Practices
- Use Actions API for complex key sequences
- Always release modifier keys (keyUp after keyDown)
- Test on target OS - key combinations vary by platform
- Add small delays if keys are sent too fast
- Verify the active element before sending keys
Next Steps
- Mouse Actions - Advanced mouse control
- Drag and Drop - Complex interactions
- Explicit Waits - Wait for keyboard effects