Select Multiple options from a drop down list

In this tutorial, I will be going to explain you the ways to select multiple options from a Drop down list using Selenium Webdriver.

Option 1 :

WebDriver provides a class called Select to perform operations on a Drop down list.

Let say, if user wants to select the first two options from above drop down. Code will be like :

WebElement selectList= driver.findElement(By.xpath("//select[@name='cars']"));
Select select = new Select(selectList);
select.selectByVisibleText("Maruti");
select.selectByVisibleText("Honda");

Above code will Select Maruti and Honda from a list. Its that simple, you just need to execute the select command one by one in order to select multiple options from a drop down list.

MultiSelect

Complete Code :

import java.util.concurrent.TimeUnit;
import org.junit.Test;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.support.ui.Select;

public class MultiSelectDropdown {

	WebDriver driver;

	@Test
	public void testApp() {
		System.setProperty("webdriver.chrome.driver",
				System.getProperty("user.dir") + "/SeleniumDrivers/chromedriver.exe");
		driver = new ChromeDriver();
		driver.get("http://automate-apps.com/select-multiple-options-from-a-drop-down-list/");
		driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
		WebElement selectList = driver.findElement(By.xpath("//select[@name='cars']"));
		Select select = new Select(selectList);
		select.selectByVisibleText("Maruti");
		select.selectByVisibleText("Honda");

	}

}

Option 2 :

Generally, if user wants to select multiple options from drop down manually, he would press CTRL and click on all the options which he wants to select.
In order to replicate the above method using automation, webdriver provides Actions class to interact with Keyboard actions. Code will be like :

WebElement option1= driver.findElement(By.xpath("//option[@value='Maruti']"));
WebElement option2= driver.findElement(By.xpath("//option[@value='Honda']"));
Actions action = new Actions(driver);
action.keyDown(Keys.CONTROL).click(option1).click(option2).build().perform();

Above code will Select Maruti and Honda from a list. What we just did here is, firstly we have put all the options which user wants to select into WebElement object and then used Actions Class to press down CTRL key and perform clicks on both options.

Complete Code :

import java.util.concurrent.TimeUnit;
import org.junit.Test;
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 MultiSelectDropdown {

	WebDriver driver;

	@Test
	public void testApp() {
		System.setProperty("webdriver.chrome.driver",
				System.getProperty("user.dir") + "/SeleniumDrivers/chromedriver.exe");
		driver = new ChromeDriver();
		driver.get("http://automate-apps.com/select-multiple-options-from-a-drop-down-list/");
		driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);

		WebElement select1 = driver.findElement(By.xpath("//option[@value='Maruti']"));
		WebElement select2 = driver.findElement(By.xpath("//option[@value='Honda']"));
		Actions action = new Actions(driver);
		action.keyDown(Keys.CONTROL).click(select1).click(select2).build().perform();

	}

}

If you really like the information provided above, please don’t forget to like us on Facebook, you can also leave the comment.

Leave a Reply

Your email address will not be published. Required fields are marked *