How to Select Multiple Elements using Actions Class in Selenium using Java?
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.
In this example program, we are navigating to the website and trying to select multiple items.
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,