Selenium - Multi Select Action


Advertisements

Sometimes we would be in a situation to select two or more items in a list box or text area. To understand the same, we would demonstrate multiple selection from the list using 'https://demos.devexpress.com/aspxeditorsdemos/ListEditors/MultiSelect.aspx'.

Example

Let us say, we want to select 3 items from this list as shown below -

selenium_ide_187

Let us see how to code for this functionality −

import java.util.List;
import java.util.concurrent.TimeUnit;

import org.openqa.selenium.*;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.interactions.Actions;
import org.openqa.selenium.interactions.Action;

public class webdriverdemo {

   public static void main(String[] args) throws InterruptedException {
   
      WebDriver driver = new FirefoxDriver();
      driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);

      driver.navigate().to("http://demos.devexpress.com/aspxeditorsdemos/ListEditors/MultiSelect.aspx");

      //driver.manage().window().maximize();
      driver.findElement(By.id("ContentHolder_lbSelectionMode_I")).click();
      driver.findElement(By.id("ContentHolder_lbSelectionMode_DDD_L_LBI1T0")).click();
      Thread.sleep(5000);
      
      // Perform Multiple Select
      Actions builder = new Actions(driver);
      WebElement select = driver.findElement(By.id("ContentHolder_lbFeatures_LBT"));
      List<WebElement> options = select.findElements(By.tagName("td"));
      
      System.out.println(options.size());
      Action multipleSelect = 
         builder.keyDown(Keys.CONTROL).click(options.get(2)).click(options.get(4)).click(options.get(6)).build();
      
      multipleSelect.perform();
      driver.close();
   }
}

output

Upon executing the script, the items would be selected as displayed above and the size of the list box would also be printed in the console.

selenium_ide_188
selenium_user_interactions.htm
Advertisements