How to Select Multiple Elements using Actions Class in Selenium using Java?
Last Updated :
28 Apr, 2025
Selenium is an open-source web automation tool that supports many user actions to perform in the web browser. Automating a web page that has file upload or other functionality which requires selecting multiple elements, to perform the multiple select actions the selenium provides a class called Actions. The Action class provides the method for Keyboard actions. The actions provided by this class are performed by an API called Advanced user interaction in selenium webdriver.
How to Select Multiple Elements?
Usually, we click the control(ctrl) button and select the multiple elements, Actions class also provides the same kind of approach to selecting the multiple elements. The Actions class provides the Keyboards actions that are used to Click and down the control key and click the multiple elements after that the control(ctrl) will be down. To perform this we are using the keyboard actions and the Keys in the selenium.
keyDown(Keys.CONTROL)
.click(element1)
.click(element2)
.build();
This is used to select the multiple elements in the webpage, for that we need to use the Actions class,
Actions action=new Actions(driver);
After creating an object for the actions class we need to use the Action method for the series of actions to be performed.
Action seriesOfActions = (Action) action.keyDown(Keys.CONTROL)
.click(element1)
.click(element2)
.build();
Now this series of actions is performed by calling the perform() method.
seriesOfActions.perform();
Example:
In this example program, we are navigating to the website and trying to select multiple items.
Java
public class Geeks {
public void geekforgeeks() throws InterruptedException {
ChromeDriver driver = new ChromeDriver();
driver.manage().window().maximize();
driver.get("https://jqueryui.com/selectable/");
WebElement iframe=driver.findElement(By.tagName("iframe"));
driver.switchTo().frame(iframe);
WebElement element1=driver.findElement(By.xpath("//li[contains(text(),'Item 1')]"));
WebElement element2=driver.findElement(By.xpath("//li[contains(text(),'Item 2')]"));
Actions action=new Actions(driver);
Action seriesOfActions = (Action) action.keyDown(Keys.CONTROL)
.click(element1)
.click(element2)
.build();
seriesOfActions.perform();
Thread.sleep(3000);
driver.close();
}
}
This code will navigate to the webpage and select the first two elements. This is performed by " keyDown(Keys. CONTROL)" it is used to click and hold the control(ctrl) key and then click operations are performed. The output of this code is,
Output:
Â
Similar Reads
How to Drag and Drop an Element using Selenium WebDriver in Java? Selenium is an open-source web automation tool that supports many user actions to perform in the web browser. Automating a modern web page that has a drag and drop functionality and drag and drop is used to upload the files and so many user activities. so to perform the drag and drop actions the sel
2 min read
How to Perform Right-Click using Java in Selenium? While automating a website for testing there is always required to perform some right-click or other user actions on the page. Â These user actions are one of the most commonly used actions during automation, so selenium provides a way to perform these user actions by the Actions class. How to Perfor
2 min read
How to Get All Available Links on the Page using Selenium in Java? Selenium is an open-source Web-Automation tool that is used to automate web Browser Testing. The major advantage of using selenium is, that it supports all major web browsers and works on all major Operating Systems, and it supports writing scripts on various languages such as Java, Â JavaScript, C#
2 min read
How to select a drop-down menu value using Selenium in Python? Prerequisite: Browser Automation Using Selenium Selenium is an effective device for controlling an internet browser through the program. It is purposeful for all browsers, works on all fundamental OS and its scripts are written in numerous languages i.e Python, Java, C#, etc, we will be using Python
2 min read
How to handle Action class in Selenium? Selenium can click buttons, type in text boxes, and eveÂn scroll through pages, all by itself! But what makes SeÂlenium awesome is a special feature calleÂd the Action class. The Action class lets SeÂlenium perform more compleÂx actions, like dragging and dropping items, or holding down a key and
6 min read