Key press in (Ctrl+A) Selenium WebDriver using java
Last Updated :
01 Oct, 2024
Selenium WebDriver is a powerful tool for automating web applications, and it offers various ways to simulate user actions such as clicking, typing, and even pressing keyboard shortcuts. One of the common actions is selecting all text or elements on a page using the "Ctrl+A" key press.
This is useful for scenarios like form filling, copying content, or modifying text fields. In Java Selenium WebDriver, you can simulate key combinations like Ctrl+A using the Actions class provided by Selenium.e.
Using Action Class
Action Class is a part of the import org.openqa.selenium.interactions.Actions package which used to control keyboard actions, mouse movements , drag & drop and mutiple sequential actions. Here we will perform CTRL+A using this Action Class.
Approach to Key press in (Ctrl+A) Selenium WebDriver using java
- First, you have to select the web element before performing actions on it.
- Then click on the element by performing actions.click(element) .
- keyDown.(Keys.CONTROL) is used to press the CTRL button.
- sendKeys("a") sends the 'A' key to the browser with the CTRL button to perform CTRL + A.
- At last keyUp.(Keys.CONTROL) & build().peform() to release the CTRL button and build and execute the action consecutively.
Example to Key press in (Ctrl+A) Selenium WebDriver using java
Here is the example of Key press in (Ctrl+A) Selenium WebDriver using Java:
Java
import java.util.concurrent.TimeUnit;
import org.openqa.selenium.By;
import org.openqa.selenium.Keys;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.interactions.Actions;
public class Ctrl_A {
public static void main(String[] args)
throws InterruptedException
{
System.setProperty(
"webdriver.chrome.driver",
"D:\\chromedriver-win64\\chromedriver-win64\\chromedriver.exe");
WebDriver driver = new ChromeDriver();
driver.get("https://www.geeksforgeeks.org/");
// Locate the element you want to select
WebElement element = driver.findElement(By.className(
"HomePageSearchContainer_homePageSearchContainer_container_input__1LS0r"));
// Create an Actions instance
Actions actions = new Actions(driver);
// Click on the element and perform Ctrl+A (select
// all)
actions.click(element)
.sendKeys("Selenium")
.keyDown(Keys.CONTROL)
.sendKeys("a")
.keyUp(Keys.CONTROL)
.build()
.perform();
// wait a bit to see the output
Thread.sleep(3000);
// Quit
driver.quit();
}
}
Output
CTRL + A using Action Class
Using sendkeys with keys.chord
keys.chord is a part of the import org.openqa.selenium. keys.chord binds multiple key actions together into a single string and sends them as a single action.Compare to Action Class it is more cmopact and ideal for this type scenarios where you need to perform multiple keyboard actions. Also remember that guys keys.chord can't perform mouse actions like Action Class.
Approach
- First you have to select the web element before performing actions on it.
- Keys.chord(Keys.CONTROL, "a") combines the "Ctrl" and "A" keys together and sends them as a single command to the element.
Example
Java
import java.util.concurrent.TimeUnit;
import org.openqa.selenium.By;
import org.openqa.selenium.Keys;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
public class Ctrl_A {
public static void main(String[] args)
throws InterruptedException
{
System.setProperty(
"webdriver.chrome.driver",
"D:\\chromedriver-win64\\chromedriver-win64\\chromedriver.exe");
WebDriver driver = new ChromeDriver();
// URL
driver.get("https://www.geeksforgeeks.org/");
driver.manage().timeouts().implicitlyWait(4, TimeUnit.SECONDS);
// identify element
WebElement element = driver.findElement(By.className(
"HomePageSearchContainer_homePageSearchContainer_container_input__1LS0r"));
// enter text then ctrl+a with Keys.chord()
element.sendKeys("Selenium");
String s = Keys.chord(Keys.CONTROL, "A");
element.sendKeys(s);
// quit
driver.quit();
}
}
Output
CTRL + A using keys.chordConclusion
In conclusion, using Ctrl+A in Selenium WebDriver helps in automating tasks that involve selecting all content within a text box or a webpage. By utilizing the Actions class, you can easily simulate complex keyboard interactions, making your tests more versatile and user-centric.
Mastering key press actions in Selenium WebDriver can significantly enhance your test automation framework, allowing for more robust and flexible test scenarios.