In the previous tutorial, it is explained how to automate Checkbox in Selenium. In this tutorial, we will learn how to handle Drop Down & Multiple Select Operations. DropDown& Multiple Select Operations works together and almost the same way. The only difference between these two is deselecting statement & multiple selections are not allow on DropDown.
To identify DropDown or multi select list on a web page, we can use any one of options like id, name, xpath, css, etc.
To perform any operation on DropDown, we need to do 2 things:-
1) Import package – org.openqa.selenium.support.ui.Select
2) Create new Select object of class Select.
Select oSelect = new Select());
We can access all the methods resides in side the SELECTclass by typing oSelect + dot.

Different Select Commands
Before we discuss about various select commands, we should know how HTML code of Drop Down actually looks

1) selectByVisibleText
selectByVisibleText(String arg0) : void – Choose the option given under any dropdowns and multiple selection boxes with selectByVisibleText method. It takes a parameter of String that is one of the Value of Select element and it returns nothing.
Syntax:
oSelect.selectByVisibleText(“text”);
To select the value Tuesday
Select mselect = new Select(driver.findElement(By.id("select-demo")));
2) selectByIndex
selectByIndex(int arg0) : void – It is almost the same as selectByVisibleText but the only difference here is that we provide the index number of the option here rather the option text. It takes a parameter of int which is the index value of Select element and it returns nothing.
Syntax:
oSelect.selectByIndex(int);
To select the value 6 using index
Select bselect = new Select(driver.findElement(By.name("birthday_day")));
bselect.selectByIndex(6);
3) selectByValue
selectByValue(String arg0) : void – It selects the option of the value. It takes a parameter of String that is of the value of Select element and it returns nothing.
Syntax:
oSelect.selectByValue(“text”);
To select the value of Wednesday
Select yselect = new Select(driver.findElement(By.id("year")));
yselect.selectByValue("Wednesday");
Let us explain this with the help of an example.
1) Launch new Browser and open “https://www.seleniumeasy.com/test/basic-select-dropdown-demo.html”
2) Select Day Drop down ( Use Id to identify the element )
3) Select option ‘Tuesday’ (Use selectByVisibleText)
4) Select option ‘6’ (Use selectByIndex)
5) Select option ‘Wednesday’ (Use selectByValue)
6) Close the browser
import java.util.concurrent.TimeUnit;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.support.ui.Select;
public class DropDown_Demo {
public static void main(String[] args) {
System.setProperty("webdriver.chrome.driver", "src\\test\\resources\\webdrivers\\window\\chromedriver.exe");
// Initiate Chrome browser
WebDriver driver = new ChromeDriver();
// Maximize the browser
driver.manage().window().maximize();
driver.manage().timeouts().implicitlyWait(20, TimeUnit.SECONDS);
// Pass application url
driver.get("https://www.seleniumeasy.com/test/basic-select-dropdown-demo.html");
// Select option 'Tuesday' (Use selectByVisibleText)
Select mselect = new Select(driver.findElement(By.id("select-demo")));
mselect.selectByVisibleText("Tuesday");
String daySelected1 = driver.findElement(By.className("selected-value")).getText();
System.out.println("Selected Day 1 :" + daySelected1);
// Select option '6' (Use selectByIndex)
Select bselect = new Select(driver.findElement(By.id("select-demo")));
bselect.selectByIndex(6);
String daySelected2 = driver.findElement(By.className("selected-value")).getText();
System.out.println("Selected Day 2:" + daySelected2);
// Select option 'Wednesday' (Use selectByValue)
Select yselect = new Select(driver.findElement(By.id("select-demo")));
yselect.selectByValue("Wednesday");
String daySelected3 = driver.findElement(By.className("selected-value")).getText();
System.out.println("Selected Day 3:" + daySelected3);
// close the browser
driver.close();
}
}


DeSelect Methods
The way we select different values of Drop Down and Multi Select, we can deselect the options similarly.
1) deselectAll
deselectAll( ) : void – Clear all selected entries. This is only valid when the SELECT supports multiple selections.
Syntax:
oSelect.deselectAll;
2) deselectByIndex
deselectByIndex(int arg0) : void –Deselect the option at the given index.
Syntax:
oSelect.deselectByIndex;
3) deselectByValue
deselectByValue(String arg0) : void –Deselect all options that have a value matching the argument.
Syntax:
oSelect.deselectByValue;
4) deselectByVisibleText
deselectByVisibleText(String arg0) : void – Deselect all options that display text matching the argument.
Syntax:
oSelect.deselectByVisibleText
Below is an example of how to operate on multi selection options.
1) Launch new Browser and open “https://www.seleniumeasy.com/test/basic-select-dropdown-demo.html”
2) Select ‘States’ Multiple selection box ( Use Name locator to identify the element )
3) Select option ‘California’ and then deselect it (Use selectByIndex and deselectByIndex)
4) Select option ‘Ohio’ and then deselect it (Use selectByVisibleText and deselectByVisibleText)
5) Print and select all the options for the selected Multiple selection list.
6) Deselect all options
7) Close the browser
import java.util.List;
import java.util.concurrent.TimeUnit;
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 MultiSelect {
public static void main(String[] args) throws InterruptedException {
System.setProperty("webdriver.chrome.driver", "src\\test\\resources\\webdrivers\\window\\chromedriver.exe");
// Initiate Chrome browser
WebDriver driver = new ChromeDriver();
// Maximize the browser
driver.manage().window().maximize();
// Put an Implicit wait and launch URL
driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
driver.get("https://www.seleniumeasy.com/test/basic-select-dropdown-demo.html");
// Use Name locator to identify the Multi Selection List
Select oSelect = new Select(driver.findElement(By.name("States")));
// Select option 'California' and then deselect it (Use selectByIndex and
// deselectByIndex)
oSelect.selectByIndex(0);
Thread.sleep(1000);
oSelect.deselectByIndex(0);
// Select option 'Ohio' and then deselect it (Use selectByVisibleText and
// deselectByVisibleText)
oSelect.selectByVisibleText("Ohio");
Thread.sleep(1000);
oSelect.deselectByVisibleText("Ohio");
// Print and select all options for the selected Multiple selection list.
List oSize = oSelect.getOptions();
int OptionSize = oSize.size();
// Setting up the loop to print all the options
for (int i= 0; i< OptionSize; i++) {
// Storing the value of the option
String sValue = oSelect.getOptions().get(i).getText();
// Printing the stored value
System.out.println(sValue);
// Selecting all the elements one by one
oSelect.selectByIndex(i);
Thread.sleep(1000);
}
// Deselect all
oSelect.deselectAll();
// close the browser
driver.close();
}
}
Output
California
Florida
New Jersey
New York
Ohio
Texas
Pennsylvania
Washington


Very easy Explaination with examples… Liked it..
LikeLike
This is very detailed blog on Dropdown. It has cleared almost all my doubts. I still have a doubt that can we use deselect in Dropdown?
LikeLike
Thanks Nidhi. We can't use deselect methods for Dropdown. If we try to use it, we will get this message – Exception in thread \”main\” java.lang.UnsupportedOperationException: You may only deselect options of a multi-select at org.openqa.selenium.support.ui.Select.deselectByVal
LikeLike