Search
  • +44-7459919437 (UK- WhatsApp & Direct Call) | +91-6383544892 (India - WhatsApp Only) | Email Id : vinothtechsolutions@gmail.com
Vinoth Tech Solutions
  • Home
  • Tutorials
  • Free Complete QA Video Courses
    • Cypress Automation
    • Cucumber BDD Framework
    • JavaScript for Playwright & Cypress Automation
    • API Manual and Automation Testing using SoapUI
    • Appium 2.0 Mobile Automation
    • DevOps CI CD using Real Time Project Setup
  • Self Paced Video Course
    • Selenium Course Curriculum
    • Playwright Course Curriculum
  • About Me
    • LinkedIn Profile
    • Placed Students Feedback
    • Online Training Feedback
    • Tech Events & Sessions
  • FAQS
  • Demo Sites
    • Practice Automation
      • Registration Form
      • Transaction Details
      • DropDown
      • Basic Authentication
      • Mouse Event
      • Keyboard Events
      • Alert and Popup
      • Multiple Windows
      • iFrames
      • Wait WebElement
      • WebTable
      • Date Picker Calendar
    • E-Commerce Demo Application
    • Healthcare Demo Page
    • Create Account Demo Page
    • XPath Demo Page
    • Python JS Online Compiler
    • Python Tutorials
  • Home
  • Tutorials
  • Free Complete QA Video Courses
    • Cypress Automation
    • Cucumber BDD Framework
    • JavaScript for Playwright & Cypress Automation
    • API Manual and Automation Testing using SoapUI
    • Appium 2.0 Mobile Automation
    • DevOps CI CD using Real Time Project Setup
  • Self Paced Video Course
    • Selenium Course Curriculum
    • Playwright Course Curriculum
  • About Me
    • LinkedIn Profile
    • Placed Students Feedback
    • Online Training Feedback
    • Tech Events & Sessions
  • FAQS
  • Demo Sites
    • Practice Automation
      • Registration Form
      • Transaction Details
      • DropDown
      • Basic Authentication
      • Mouse Event
      • Keyboard Events
      • Alert and Popup
      • Multiple Windows
      • iFrames
      • Wait WebElement
      • WebTable
      • Date Picker Calendar
    • E-Commerce Demo Application
    • Healthcare Demo Page
    • Create Account Demo Page
    • XPath Demo Page
    • Python JS Online Compiler
    • Python Tutorials
View Categories
  • Home
  • Tutorials
  • Selenium Automation Testing
  • Handling Keyboard Events

Handling Keyboard Events

Handling Keyboard Events

Selenium’s Actions class and the sendKeys() method allow you to simulate various keyboard events.

This is useful for automation scenarios such as filling forms, submitting data, simulating hotkeys, and navigating through elements.

Common Keyboard Event Scenarios in Selenium

1. Typing Text

  • Use case: Enter data into input fields.
πŸ“„
filename.js
WebElement input = driver.findElement(By.id("username"));

input.sendKeys("VinothQA");

2. Pressing Special Keys (e.g., ENTER, TAB, BACKSPACE)

  • Use case: Submit a form, navigate fields, delete text.
πŸ“„
filename.js
input.sendKeys(Keys.ENTER);

input.sendKeys(Keys.TAB);

input.sendKeys(Keys.BACK_SPACE);

3. Keyboard Shortcuts (e.g., Ctrl+A, Ctrl+C, Ctrl+V)

  • Use case: Select all, copy, paste.
πŸ“„
filename.js
import org.openqa.selenium.Keys;

import org.openqa.selenium.interactions.Actions;

Actions actions = new Actions(driver);

WebElement input = driver.findElement(By.id("textField"));

// Ctrl + A (Select All)

actions.keyDown(Keys.CONTROL).sendKeys("a").keyUp(Keys.CONTROL).perform();

// Ctrl + C (Copy)

actions.keyDown(Keys.CONTROL).sendKeys("c").keyUp(Keys.CONTROL).perform();

// Ctrl + V (Paste)

actions.keyDown(Keys.CONTROL).sendKeys("v").keyUp(Keys.CONTROL).perform();

4. Simulating Key Combinations (SHIFT, ALT)

  • Use case: Capitalizing text, accessing browser or app shortcuts.
πŸ“„
filename.js
// SHIFT + key for capital letter

actions.keyDown(Keys.SHIFT).sendKeys("v").keyUp(Keys.SHIFT).perform();

5. Clearing a Field

  • Use case: Remove all existing text.
πŸ“„
filename.js
input.clear(); // Preferred method

// or with keyboard:

input.sendKeys(Keys.CONTROL + "a", Keys.DELETE);

6. Navigating Menus Using Arrow Keys

  • Use case: Moving through dropdowns or lists.
πŸ“„
filename.js
input.sendKeys(Keys.ARROW_DOWN);

input.sendKeys(Keys.ARROW_UP);

7. Submitting a Form

  • Use case: Submitting without clicking a button.
πŸ“„
filename.js
input.sendKeys(Keys.ENTER);

// or

input.submit();
Selenium Automation Testing
What are your Feelings

Share This Article :

  • Facebook
  • X
  • LinkedIn
Handling Mouse EventsHandling Alert Popup
Β© 2025 V-Tech Solutions Ltd (UK), Reg. No: 16489105