Selenium WebDriver

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

act.sendKeys(wb, "text")

keyDown() Method

Press modifier keys (CTRL, SHIFT, ALT, etc.)

act.keyDown(Keys.CONTROL)

keyUp() Method

Release pressed modifier keys

act.keyUp(Keys.CONTROL)

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

package Tutorial11;

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

// Enter text and delete last 3 characters
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

// Enter text, go to beginning, delete first 3 characters
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
WebElement wb = driver.findElement(By.name("ctl00$ContentPlaceHolder1$txtFName"));
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

act.keyDown(Keys.HOME).build().perform();

END Key

Navigate to the bottom of the page

act.keyDown(Keys.END).build().perform();
// Example: Navigate to end and then back to home
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

// Scroll down multiple times
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

WebElement wb = driver.findElement(By.name("ctl00$ContentPlaceHolder1$ddlTitle"));
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

  1. Click on dropdown to open it
  2. Press ARROW_DOWN to move to next option
  3. Press ARROW_DOWN again to move to "Ms."
  4. Release ARROW_DOWN key
  5. Press ENTER to select the option
  6. 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.

Day 19 Knowledge Check

Question 1 of 5

How many ways can you enter text in a text box using Selenium?

SDET Mastery

Master Test Automation

Home
CurriculumPracticeQ&ACheatsheet
🍵Buy me a Chai

Automation Testing Course

Comprehensive course covering Manual Testing, Java Programming, and Selenium WebDriver

🍵Buy me a Chai
Privacy Policy•GitHub
© 2024 Automation Testing Course. All rights reserved.