How to Press Keyboard Keys in Selenium: Syntax and Examples
In Selenium, you can press keyboard keys using the
sendKeys() method on a web element or the Actions class for special keys. Use Keys enum to represent keys like ENTER, TAB, or CONTROL when sending key presses.Syntax
To press keys in Selenium, you mainly use two approaches:
- sendKeys(): Sends key presses to a specific web element.
- Actions class: Used for complex keyboard actions like key down, key up, or combined keys.
The Keys enum provides constants for special keys like ENTER, TAB, CONTROL, etc.
java
import org.openqa.selenium.Keys; import org.openqa.selenium.WebDriver; import org.openqa.selenium.WebElement; import org.openqa.selenium.interactions.Actions; // Using sendKeys on a WebElement webElement.sendKeys(Keys.ENTER); // Using Actions class for key press Actions actions = new Actions(driver); actions.keyDown(Keys.CONTROL).sendKeys("a").keyUp(Keys.CONTROL).perform();
Example
This example shows how to open a browser, find a text input, type text, and press the ENTER key using sendKeys(). It also demonstrates using Actions to press CTRL+A to select all text.
java
import org.openqa.selenium.By; import org.openqa.selenium.Keys; import org.openqa.selenium.WebDriver; import org.openqa.selenium.WebElement; import org.openqa.selenium.chrome.ChromeDriver; import org.openqa.selenium.interactions.Actions; public class KeyboardExample { public static void main(String[] args) { System.setProperty("webdriver.chrome.driver", "/path/to/chromedriver"); WebDriver driver = new ChromeDriver(); try { driver.get("https://www.google.com"); WebElement searchBox = driver.findElement(By.name("q")); // Type text and press ENTER searchBox.sendKeys("Selenium WebDriver" + Keys.ENTER); // Use Actions to select all text in the search box Actions actions = new Actions(driver); actions.keyDown(Keys.CONTROL).sendKeys("a").keyUp(Keys.CONTROL).perform(); Thread.sleep(2000); // Wait to see the effect } catch (InterruptedException e) { e.printStackTrace(); } finally { driver.quit(); } } }
Output
The browser opens Google, types 'Selenium WebDriver' in the search box, presses ENTER to search, then selects all text in the search box.
Common Pitfalls
Common mistakes when pressing keys in Selenium include:
- Trying to send keys to a non-interactive element, causing errors.
- Not importing or using the
Keysenum for special keys. - Forgetting to call
perform()when usingActionsclass, so the action does not execute. - Using
sendKeys()on the driver instead of a web element.
java
import org.openqa.selenium.Keys; import org.openqa.selenium.WebElement; import org.openqa.selenium.interactions.Actions; // Wrong: sending keys to driver (no effect) // driver.sendKeys(Keys.ENTER); // This will cause a compile error // Right: send keys to a web element webElement.sendKeys(Keys.ENTER); // Wrong: forgetting perform() Actions actions = new Actions(driver); actions.keyDown(Keys.CONTROL).sendKeys("a").keyUp(Keys.CONTROL); // No perform(), so no action // Right: call perform() actions.keyDown(Keys.CONTROL).sendKeys("a").keyUp(Keys.CONTROL).perform();
Quick Reference
| Action | Code Example | Description |
|---|---|---|
| Press ENTER key | webElement.sendKeys(Keys.ENTER); | Sends ENTER key to a web element |
| Press TAB key | webElement.sendKeys(Keys.TAB); | Sends TAB key to move focus |
| CTRL + A (Select All) | actions.keyDown(Keys.CONTROL).sendKeys("a").keyUp(Keys.CONTROL).perform(); | Uses Actions to press CTRL and 'a' together |
| Type text | webElement.sendKeys("text"); | Types normal text into input |
| Perform actions | actions.perform(); | Executes the built Actions sequence |
Key Takeaways
Use sendKeys() on web elements to simulate typing and key presses.
Use the Keys enum for special keys like ENTER, TAB, and CONTROL.
Use Actions class with perform() for complex keyboard actions.
Always send keys to interactive elements, not the driver directly.
Remember to call perform() when using Actions to execute the key events.