• Selenium Video Tutorials

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.

Selenium Keyboard Events 1

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 −

Selenium Keyboard Events 2

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.

Selenium Keyboard Events 3

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:.

Selenium Keyboard Events 4

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.

Advertisements