Keyboard Events & Actions Class
Master keyboard events using Actions class including sendKeys, keyDown, keyUp operations, text manipulation, and navigation keys.
Multiple Ways to Perform Operations
Different Approaches in Selenium
Enter Text (3 Ways)
- 1. WebElement sendKeys()
- 2. Actions class sendKeys()
- 3. JavaScript Executor
Click Element (5 Ways)
- 1. WebElement click()
- 2. WebElement submit()
- 3. Actions class click()
- 4. Actions class ENTER
- 5. JavaScript Executor
Handle Dropdown (6 Ways)
- 1. selectByVisibleText()
- 2. selectByIndex()
- 3. selectByValue()
- 4. Iterating values
- 5. Actions class
- 6. JavaScript Executor
Open URL (3 Ways)
- 1. get() method
- 2. navigate().to() method
- 3. JavaScript Executor interface
Keyboard Events in Actions Class
Keyboard Event Methods
sendKeys() Method
Enter text in text box / Send keys to web element
keyDown() Method
Press modifier keys (CTRL, SHIFT, ALT, etc.)
keyUp() Method
Release pressed modifier keys
Note: All keyboard event methods are present in Actions class and return Actions class.
sendKeys() Method Example
Example: https://www.saucedemo.com/
Enter username using Actions class sendKeys() method
import org.openqa.selenium.By;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.interactions.Actions;
public class Demo1 {
public static void main(String[] args) {
ChromeDriver driver = new ChromeDriver();
driver.get("https://www.saucedemo.com/");
WebElement wb = driver.findElement(By.name("user-name"));
Actions act = new Actions(driver);
act.sendKeys(wb, "standard_user").build().perform();
}
}
Text Manipulation Operations
Delete Characters from Text Box
Delete Last Characters
Use BACK_SPACE key to delete characters from the end
act.sendKeys(wb, "Rahul")
.keyDown(Keys.BACK_SPACE)
.keyDown(Keys.BACK_SPACE)
.keyDown(Keys.BACK_SPACE)
.build().perform();
Delete First Characters
Navigation + Delete Approach
Use HOME key to navigate to beginning, then DELETE key
act.sendKeys(wb, "anjali")
.keyDown(Keys.HOME)
.keyUp(Keys.HOME)
.keyDown(Keys.DELETE)
.keyDown(Keys.DELETE)
.keyDown(Keys.DELETE)
.keyUp(Keys.DELETE)
.build().perform();
Copy-Paste Operations
Copy Text from One Field to Another
Example: https://copyright.gov.in/UserRegistration/frmNewUser.aspx
Copy first name and paste in last name field
Keyboard Shortcuts Used
- • CTRL + A = Select all text
- • CTRL + C = Copy selected text
- • TAB = Move to next field
- • CTRL + V = Paste copied text
Actions act = new Actions(driver);
act.sendKeys(wb, "Pavan")
.keyDown(Keys.CONTROL).sendKeys("a")
.keyDown(Keys.CONTROL).sendKeys("c")
.keyUp(Keys.CONTROL)
.keyDown(Keys.TAB)
.keyUp(Keys.TAB)
.keyDown(Keys.CONTROL).sendKeys("v")
.keyUp(Keys.CONTROL)
.build()
.perform();
Page Navigation Keys
HOME and END Keys
HOME Key
Navigate to the top of the page
END Key
Navigate to the bottom of the page
ChromeDriver driver = new ChromeDriver();
driver.get("https://www.redbus.in/");
Actions act = new Actions(driver);
act.keyDown(Keys.END).build().perform();
Thread.sleep(4000);
act.keyDown(Keys.HOME).build().perform();
Arrow Keys for Scrolling
ARROW_DOWN Key
Scroll down the page step by step
act.keyDown(Keys.ARROW_DOWN).build().perform();
act.keyDown(Keys.ARROW_DOWN).build().perform();
act.keyDown(Keys.ARROW_DOWN).build().perform();
// ... continue as needed
Dropdown Handling with Actions Class
Select Dropdown Options Using Keyboard
Example: Select "Ms." from Title Dropdown
Use ARROW_DOWN keys to navigate and ENTER to select
Actions act = new Actions(driver);
act.click(wb)
.keyDown(Keys.ARROW_DOWN)
.keyDown(Keys.ARROW_DOWN)
.keyUp(Keys.ARROW_DOWN)
.keyDown(Keys.ENTER)
.keyUp(Keys.ENTER)
.build()
.perform();
Steps Explained
- Click on dropdown to open it
- Press ARROW_DOWN to move to next option
- Press ARROW_DOWN again to move to "Ms."
- Release ARROW_DOWN key
- Press ENTER to select the option
- Release ENTER key
Alternative Approach
Robot Class in Java
Robot Class in Java can also be used to perform Mouse and Keyboard Events as an alternative to Actions class.