- Selenium Tutorial
- Selenium - Home
- Selenium - Overview
- Selenium - Components
- Selenium - Environment Setup
- Selenium - Remote Control
- Selenium - IntelliJ
- Selenium - XPath
- Selenium IDE Tutorial
- Selenium - IDE Introduction
- Selenium - Features
- Selenium - Limitations
- Selenium - Installation
- Selenium - Creating Tests
- Selenium - Creating Script
- Selenium - Store Variables
- Selenium - Selenese Commands
- Selenium - Actions Commands
- Selenium - Accessors Commands
- Selenium - Assertions Commands
- Selenium - Assert/Verify Methods
- Selenium - Locating Strategies
- Selenium - Script Debugging
- Selenium - Verification Points
- Selenium - Pattern Matching
- Selenium - Browser Execution
- Selenium - User Extensions
- Selenium - Code Export
- Selenium - JavaScript Functions
- Selenium WebDriver Tutorial
- Selenium - Introduction
- Selenium WebDriver vs RC
- Selenium - Installation
- Selenium - First Test Script
- Selenium - Browser Options
- Selenium - Chrome Options
- Selenium - Edge Options
- Selenium - Firefox Options
- Selenium - Safari Options
- Selenium Web Elements
- Selenium - File Upload
- Selenium - Locator Strategies
- Selenium - Relative Locators
- Selenium - Finders
- Selenium - Find All Links
- Selenium - User Interactions
- Selenium - WebElement Commands
- Selenium - Browser Interactions
- Selenium - Browser Commands
- Selenium - Browser Navigation
- Selenium - Alerts & Popups
- Selenium - Handling Forms
- Selenium - Windows and Tabs
- Selenium - Handling Links
- Selenium - Input Boxes
- Selenium - Radio Button
- Selenium - Checkboxes
- Selenium - Dropdown Box
- Selenium - Handling IFrames
- Selenium - Handling Cookies
- Selenium - Date Time Picker
- Selenium - Dynamic Web Tables
- Selenium - Actions Class
- Selenium - Action Class
- Selenium - Keyboard Events
- Selenium - Key Up/Down
- Selenium - Copy and Paste
- Selenium - Handle Special Keys
- Selenium - Mouse Events
- Selenium - Drag and Drop
- Selenium - Scroll Operations
- Selenium - Waiting Strategies
- Selenium - Explicit/Implicit Wait
- Selenium - Support Features
- Selenium - Multi Select
- Selenium - Wait Support
- Selenium - Select Support
- Selenium - Color Support
- Selenium - Errors & Logging
- Selenium - Log4j Logging
- Selenium - Logging
- Selenium - Exception Handling
- Selenium - Miscellaneous
- Selenium - Handling Ajax Calls
- Selenium - JSON Data File
- Selenium - CSV Data File
- Selenium - Excel Data File
- Selenium - Cross Browser Testing
- Selenium - Multi Browser Testing
- Selenium - Multi Windows Testing
- Selenium - JavaScript Executor
- Selenium - Headless Execution
- Selenium - Capture Screenshots
- Selenium - Capture Videos
- Selenium - Page Object Model
- Selenium - Page Factory
- Selenium - Record & Playback
- Selenium Grid Tutorial
- Selenium - Overview
- Selenium Reporting Tools
- Selenium - Reporting Tools
- Selenium - TestNG
- Selenium - JUnit
- Selenium - Allure
- Selenium & other Technologies
- Selenium - Java Tutorial
- Selenium - Python Tutorial
- Selenium - C# Tutorial
- Selenium - Javascript Tutorial
- Selenium - Kotlin Tutorial
- Selenium - Ruby  Tutorial
- Selenium - Maven & Jenkins
- Selenium - Database Testing
- Selenium - LogExpert Logging
- Selenium - Log4j Logging
- Selenium - Robot API
- Selenium - AutoIT
- Selenium - Flash Testing
- Selenium - Apache Ant
- Selenium - Github
- Selenium - SoapUI
- Selenium - Cucumber
- Selenium - IntelliJ
- Selenium - XPath
- Selenium Miscellaneous Concepts
- Selenium - SSL Certificate Error
- Selenium - Alternatives
- Selenium Useful Resources
- Selenium - Quick Guide
- Selenium - Useful Resources
- Selenium - Automation Practice
- Selenium - Discussion
Selenium Webdriver - Keyboard Events
Selenium Webdriver can be used to perform keyboard events operations like key up, and down, enter multiple characters in the middle other operations, and copy and paste operations using the Actions class. The methods keyUp(), keyDown(), and sendKeys() are used to perform these operations.
Some of the methods of keyboard events are discussed below −
keyDown(CharSequence key) − This method is used to perform a modifier key press, passed as a parameter.
keyDown(WebElement e, CharSequence key) − This method is used to perform a modifier key press post focusing an element. The webElement e, and key to be pressed are passed as parameters.
keyUp(CharSequence key) − This method is used to perform a modifier key release, passed as a parameter.
keyUp(WebElement e, CharSequence key) − This method is used to perform a modifier key release post focusing an element. The webElement e, and key to be released are passed as parameters.
sendKeys(CharSequence key) − This method is used to send keys to elements in focus. The key to be sent is passed as a parameter.
sendKeys(WebElement e, CharSequence key) − This method is used to send keys to the webElement passed as parameter.
build() − This method is used to create a combination of actions having all the actions to be carried on.
perform() − This method is used to perform actions without invoking the build() first.
Please note that, while using the methods of Actions class, we would need to add the import statement −
import org.openqa.selenium.interactions.Actions in our tests.
Let us now discuss the identification of elements where the copy and paste operations are to be performed on a web page as shown in the below image. First, we would need to right click on the web page, and then click on the Inspect button in the Chrome browser. Then, the corresponding HTML code for the whole page would be visible. For investigating both the source and destination elements on that web page, we would need to click on the left upward arrow, available to the top of the visible HTML code as highlighted below.
Let us take an example on the below page, where we would first enter the text - Selenium beside the Full Name: in the first input box(highlighted) then copy and paste the same text in another input box(highlighted) beside the Last Name −
Syntax
WebDriver driver = new ChromeDriver(); // Identify the first input box with xpath locator WebElement e = driver.findElement(By.xpath("<value of xpath>")); // enter some text e.sendKeys("Selenium"); // chose the key as per platform Keys k = Platform.getCurrent().is(Platform.MAC) ? Keys.COMMAND : Keys.CONTROL; // object of Actions class to copy then paste Actions a = new Actions(driver); a.keyDown(k); a.sendKeys("a"); a.keyUp(k); a.build().perform(); // Actions class methods to copy text a.keyDown(k); a.sendKeys("c"); a.keyUp(k); a.build().perform(); // Action class methods to tab and reach to next input box a.sendKeys(Keys.TAB); a.build().perform(); // Actions class methods to paste text a.keyDown(k); a.sendKeys("v"); a.keyUp(k); a.build().perform(); // Identify the second input box with xpath locator WebElement s = driver.findElement(By.xpath("<value of xpath>")); // Getting text in the second input box String text = s.getAttribute("value"); System.out.println("Value copied and pasted: " + text);
Example
Code Implementation on CopyPasteAction.java class file.
package org.example; import org.openqa.selenium.*; import org.openqa.selenium.chrome.ChromeDriver; import org.openqa.selenium.interactions.Actions; import java.util.concurrent.TimeUnit; public class CopyPasteAction { public static void main(String[] args) throws InterruptedException { // Initiate the Webdriver WebDriver driver = new ChromeDriver(); // adding implicit wait of 15 secs driver.manage().timeouts().implicitlyWait(15, TimeUnit.SECONDS); // Opening the webpage where we will identify an element driver.get("https://www.tutorialspoint.com/selenium/practice/register.php"); // Identify the first input box with xpath locator WebElement e = driver.findElement(By.xpath("//*[@id='firstname']")); // enter some text e.sendKeys("Selenium"); // chose the key as per platform Keys k = Platform.getCurrent().is(Platform.MAC) ? Keys.COMMAND : Keys.CONTROL; // object of Actions class to copy then paste Actions a = new Actions(driver); a.keyDown(k); a.sendKeys("a"); a.keyUp(k); a.build().perform(); // Actions class methods to copy text a.keyDown(k); a.sendKeys("c"); a.keyUp(k); a.build().perform(); // Action class methods to tab and reach to next input box a.sendKeys(Keys.TAB); a.build().perform(); // Actions class methods to paste text a.keyDown(k); a.sendKeys("v"); a.keyUp(k); a.build().perform(); // Identify the second input box with xpath locator WebElement s = driver.findElement(By.xpath("//*[@id='lastname']")); // Getting text in the second input box String text = s.getAttribute("value"); System.out.println("Value copied and pasted: " + text); // Closing browser driver.quit(); } }
Output
Value copied and pasted: Selenium Process finished with exit code 0
In the above example, we had first entered the text Selenium in the first input box and then copied and pasted the same text in the second input box.
Finally, we had obtained the entered text in the second input box as a message in the console - Value copied and pasted: Selenium.
Finally, the message Process finished with exit code 0 was received, signifying successful execution of the code.
Let us take another example, where we would enter text Selenium within the input box beside Full Name: which in focus.
Syntax
WebDriver driver = new ChromeDriver(); // Identify the input box with xpath locator WebElement e = driver.findElement(By.xpath("<value of xpath>")); // object of Actions class to input text in focus Actions a = new Actions(driver); a.click(e).sendKeys("Selenium").build().perform();
Example
Code Implementation on EnterTextInFocus.java class file.
package org.example; import org.openqa.selenium.*; import org.openqa.selenium.chrome.ChromeDriver; import org.openqa.selenium.interactions.Actions; import java.util.concurrent.TimeUnit; public class EnterTextInFocus { public static void main(String[] args) throws InterruptedException { // Initiate the Webdriver WebDriver driver = new ChromeDriver(); // adding implicit wait of 15 secs driver.manage().timeouts().implicitlyWait(15, TimeUnit.SECONDS); // Opening the webpage where we will identify an element driver.get("https://www.tutorialspoint.com/selenium/practice/text-box.php"); // Identify the first input box with xpath locator WebElement e = driver.findElement(By.xpath("//*[@id='fullname']")); // object of Actions class to input text in focus Actions a = new Actions(driver); a.click(e).sendKeys("Selenium").build().perform(); // Getting text in the second input box String text = e.getAttribute("value"); System.out.println("Value entered to input box in focus: " + text); // Closing browser driver.quit(); } }
Output
Value entered to input box in focus: Selenium Process finished with exit code 0
In the above example, we had first entered the text Selenium in the input box in focus and then obtained the text as a message in the console - Value entered to input box in focus: Selenium.
Finally, the message Process finished with exit code 0 was received, signifying successful execution of the code.
Let us take another example, where we would enter text Selenium within a designated input box beside Name:.
Syntax
WebDriver driver = new ChromeDriver(); // Identify the input box with xpath locator WebElement e = driver.findElement(By.xpath("<value of xpath>")); // object of Actions class to input text in focus Actions a = new Actions(driver); a.sendKeys(e, "Selenium").build().perform();
Example
Code Implementation on EnterText.java class file.
package org.example; import org.openqa.selenium.*; import org.openqa.selenium.chrome.ChromeDriver; import org.openqa.selenium.interactions.Actions; import java.util.concurrent.TimeUnit; public class EnterText { public static void main(String[] args) throws InterruptedException { // Initiate the Webdriver WebDriver driver = new ChromeDriver(); // adding implicit wait of 15 secs driver.manage().timeouts().implicitlyWait(15, TimeUnit.SECONDS); // Opening the webpage where we will identify an element driver.get("https://www.tutorialspoint.com/selenium/practice/selenium_automation_practice.php"); // Identify input box with xpath locator WebElement e = driver.findElement(By.xpath("//*[@id='name']")); // object of Actions class to input text Actions a = new Actions(driver); a.sendKeys(e,"Selenium").build().perform(); // Getting text in the second input box String text = e.getAttribute("value"); System.out.println("Value entered to input box: " + text); // Closing browser driver.quit(); } }
Output
Value entered to input box: Selenium Process finished with exit code 0
In the above example, we had entered the text Selenium in an input box and then obtained the text as a message in the console - Value entered to input box: Selenium.
Finally, the message Process finished with exit code 0 was received, signifying successful execution of the code.
In this article, we discussed how to perform keyboard events using Selenium Webdriver.