Skip to content
Related Articles
Open in App
Not now

Related Articles

How to Select Multiple Elements using Actions Class in Selenium using Java?

Improve Article
Save Article
  • Last Updated : 15 Nov, 2022
Improve Article
Save Article

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:

 

My Personal Notes arrow_drop_up
Related Articles

Start Your Coding Journey Now!