Select Multiple Options from a Dropdown List

In this tutorial, we will learn about the ways to select multiple options from a Dropdown List using Selenium Webdriver.

Sample DropDown List:

Option 1:

Selenium WebDriver provides a class called Select which is used to select single or multiple options from Dropdown list.

Let say, if you want to select the first two options (Maruti & Honda) from above List. Code would be like :

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

Note: You need to import Selenium’s Select class in order to access it using below code line:

import org.openqa.selenium.support.ui.Select;

In above code, we have created an object of Select class and passed the Dropdown List as a parameter and after that we have called the selectByVisibleText() function to select the options using visible text.

Output for above code would be:

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 :

Ok, What you do when you manually select the multiple options from dropdown list?

You just press CTRL button from Keyboard and then click on the options which you want to select. In order to replicate the same in automation, Selenium Webdriver provides Actions class to interact with Keyboard actions. Code would 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 options. What we just did here is that we have stored the options which we want to select into WebElement object and then created an object of Actions Class to press down the CTRL key and then click 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 *


Notice: Undefined offset: 0 in G:\PleskVhosts\automate-apps.com\httpdocs\wp-content\plugins\cardoza-facebook-like-box\cardoza_facebook_like_box.php on line 924