Select Box

In HTML, when a user required to select a input from given list of options, select box is used. It's an element that has a drop-down list of options. The user can select required options from the list. There are two types of select boxes ie single select to select single user input and multi select for multiple user inputs.

Below are the HTML code for single select box.


HTML
Copy
<select name="oldCars">
<option value="volvo">Volvo</option>
<option value="saab">Saab</option>
<option value="opel">Opel</option>
<option value="audi">Audi</option>
</select>

Below are the HTML code for multi select box.


HTML
Copy
<select name="cars" multiple="" class="multipleCombo">
  <option value="volvo">Volvo</option>
  <option value="saab">Saab</option>
  <option value="opel">Opel</option>
  <option value="audi">Audi</option>
  <option value="Merc">Merc</option>
  <option value="bmw">BMW</option>
</select>

Following are the different commands to interact with the select box.

isMultiple

Sometimes we need to check if the select box element support selecting multiple options or just a single options at a time. This is done by checking the value of the "multiple" attribute. If this method returns true, it support multiple selection of options.
To work with select box we need to create an object of the Select class by passing the web element of the select box. Lets look at the code for select box.

Java
C#
Python
Javascript
Copy
WebDriver driver = new ChromeDriver();
driver.get("https://www.testautomationstudio.com/demo/set/");
driver.manage().timeouts().implicitlyWait(Duration.ofSeconds(30));
// Find the select box element
WebElement selectBoxelem = driver.findElement(By.name("oldCars"));
//Create select box object
Select dropdownObject = new Select(selectBoxelem);
//Check if select box is multiple select
boolean isMultipleSelect = dropdownObject.isMultiple();
Copy
IWebDriver driver = new ChromeDriver();
driver.Url = "https://www.testautomationstudio.com/demo/set/";
driver.Manage().Window.Maximize();
driver.Manage().Timeouts().ImplicitWait = TimeSpan.FromSeconds(30);
IWebElement selectBoxelem = driver.FindElement(By.Name("oldCars"));
//Create select box object
SelectElement dropdownObject = new SelectElement(selectBoxelem);
//Check if select box is multiple select
bool isMultipleSelect = dropdownObject.IsMultiple;
Python code coming soon
Javascript code coming soon

selectByVisibleText

The method selectByVisibleText will select all the option available in the list that displayed with text matching to the given argument. In the following example we have given argument as Opel, this would select an option having inner text equals to Opel. It will throw NoSuchElementException error, if no matching option elements are found.


Java
C#
Python
Javascript
Copy
WebDriver driver = new ChromeDriver();
driver.get("https://www.testautomationstudio.com/demo/set/");
driver.manage().timeouts().implicitlyWait(Duration.ofSeconds(30));
// Find the select box element
WebElement selectBoxelem = driver.findElement(By.name("oldCars"));
//Create select box object
Select dropdownObject = new Select(selectBoxelem);
//Select option which is visible in the list
dropdownObject.selectByVisibleText("Opel");
Copy
IWebDriver driver = new ChromeDriver();
driver.Url = "https://www.testautomationstudio.com/demo/set/";
driver.Manage().Window.Maximize();
driver.Manage().Timeouts().ImplicitWait = TimeSpan.FromSeconds(30);
IWebElement selectBoxelem = driver.FindElement(By.Name("oldCars"));
//Create select box object
SelectElement dropdownObject = new SelectElement(selectBoxelem);
//Select option which is visible in the list
dropdownObject.SelectByText("Opel");
Python code coming soon
Javascript code coming soon

selectByIndex

The method selectByIndex will select the option at the given index. This is done by examining the index attribute of an element. The index starts from 0. It will throw NoSuchElementException error, if no matching option elements are found.

Java
C#
Python
Javascript
Copy
WebDriver driver = new ChromeDriver();
driver.get("https://www.testautomationstudio.com/demo/set/");
driver.manage().timeouts().implicitlyWait(Duration.ofSeconds(30));
// Find the select box element
WebElement selectBoxelem = driver.findElement(By.name("oldCars"));
//Create select box object
Select dropdownObject = new Select(selectBoxelem);
//Select option by its index. At index 0 we have Volvo.
dropdownObject.selectByIndex(0);
Copy
IWebDriver driver = new ChromeDriver();
driver.Url = "https://www.testautomationstudio.com/demo/set/";
driver.Manage().Window.Maximize();
driver.Manage().Timeouts().ImplicitWait = TimeSpan.FromSeconds(30);
IWebElement selectBoxelem = driver.FindElement(By.Name("oldCars"));
//Create select box object
SelectElement dropdownObject = new SelectElement(selectBoxelem);
//Select option by its index. At index 0 we have Volvo.
dropdownObject.SelectByIndex(0);
Python code coming soon
Javascript code coming soon

selectByValue

The method selectByValue will select all the option that have a value matching the given argument. In the following example we have given argument as saab, this would select an option with value equals to saab ie option Saab. It will throw NoSuchElementException error, if no matching option elements are found.

Java
C#
Python
Javascript
Copy
WebDriver driver = new ChromeDriver();
driver.get("https://www.testautomationstudio.com/demo/set/");
driver.manage().timeouts().implicitlyWait(Duration.ofSeconds(30));
// Find the select box element
WebElement selectBoxelem = driver.findElement(By.name("oldCars"));
//Create select box object
Select dropdownObject = new Select(selectBoxelem);
//Select option by its value
dropdownObject.selectByValue("saab");
Copy
IWebDriver driver = new ChromeDriver();
driver.Url = "https://www.testautomationstudio.com/demo/set/";
driver.Manage().Window.Maximize();
driver.Manage().Timeouts().ImplicitWait = TimeSpan.FromSeconds(30);
IWebElement selectBoxelem = driver.FindElement(By.Name("oldCars"));
//Create select box object
SelectElement dropdownObject = new SelectElement(selectBoxelem);
//Select option by its value
dropdownObject.SelectByValue("saab");
Python code coming soon
Javascript code coming soon

getOptions

This method will return all options belonging to this select tag. The return type will be list of web elements.

Java
C#
Python
Javascript
Copy
WebDriver driver = new ChromeDriver();
driver.get("https://www.testautomationstudio.com/demo/set/");
driver.manage().timeouts().implicitlyWait(Duration.ofSeconds(30));
// Find the select box element
WebElement selectBoxelem = driver.findElement(By.name("oldCars"));
//Create select box object
Select dropdownObject = new Select(selectBoxelem);
//To get all options in select box
List<WebElement> optionsList = dropdownObject.getOptions();
for (WebElement option : optionsList )
{
	System.out.println(option.getText());
}
Copy
IWebDriver driver = new ChromeDriver();
driver.Url = "https://www.testautomationstudio.com/demo/set/";
driver.Manage().Window.Maximize();
driver.Manage().Timeouts().ImplicitWait = TimeSpan.FromSeconds(30);
IWebElement selectBoxelem = driver.FindElement(By.Name("oldCars"));
//Create select box object
SelectElement dropdownObject = new SelectElement(selectBoxelem);
//To get all options in select box
IList<IWebElement> optionsList = dropdownObject.Options;
foreach (IWebElement option in optionsList)
{
    Console.WriteLine(option.Text);
}
driver.Quit();
Python code coming soon
Javascript code coming soon

getAllSelectedOptions

This method will return all selected options belonging to this select tag. The return type will be list of web elements.

Java
C#
Python
Javascript
Copy
WebDriver driver = new ChromeDriver();
driver.get("https://www.testautomationstudio.com/demo/set/");
driver.manage().timeouts().implicitlyWait(Duration.ofSeconds(30));
// Find the select box element
WebElement selectBoxelem = driver.findElement(By.name("oldCars"));
//Create select box object
Select dropdownObject = new Select(selectBoxelem);
//To get all the selected options in select box
List<WebElement> selectedOptionList = dropdownObject.getAllSelectedOptions();
for (WebElement option : selectedOptionList )
{
	System.out.println(option.getText());
}
Copy
IWebDriver driver = new ChromeDriver();
driver.Url = "https://www.testautomationstudio.com/demo/set/";
driver.Manage().Window.Maximize();
driver.Manage().Timeouts().ImplicitWait = TimeSpan.FromSeconds(30);
IWebElement selectBoxelem = driver.FindElement(By.Name("oldCars"));
//Create select box object
SelectElement dropdownObject = new SelectElement(selectBoxelem);
//To get all selected options in select box
IList<IWebElement> selectedOptionList = dropdownObject.AllSelectedOptions;
foreach (IWebElement option in selectedOptionList)
{
    Console.WriteLine(option.Text);
}
driver.Quit();
Python code coming soon
Javascript code coming soon

getFirstSelectedOption

This method will return the first selected option in this select tag if it is a multi select box else currently selected option in a normal select.

Java
C#
Python
Javascript
Copy
WebDriver driver = new ChromeDriver();
driver.get("https://www.testautomationstudio.com/demo/set/");
driver.manage().timeouts().implicitlyWait(Duration.ofSeconds(30));
// Find the select box element
WebElement selectBoxelem = driver.findElement(By.name("oldCars"));
//Create select box object
Select dropdownObject = new Select(selectBoxelem);
//To get first selected option in select box
WebElement firstSelectedOption = dropdownObject.getFirstSelectedOption();
System.out.println(firstSelectedOption.getText());
Copy
IWebDriver driver = new ChromeDriver();
driver.Url = "https://www.testautomationstudio.com/demo/set/";
driver.Manage().Window.Maximize();
driver.Manage().Timeouts().ImplicitWait = TimeSpan.FromSeconds(30);
IWebElement selectBoxelem = driver.FindElement(By.Name("oldCars"));
//Create select box object
SelectElement dropdownObject = new SelectElement(selectBoxelem);
//To get first selected options in select box
IWebElement firstSelectedOptionList = dropdownObject.SelectedOption;
Console.WriteLine(firstSelectedOptionList.Text);
driver.Quit();
Python code coming soon
Javascript code coming soon

deselectAll

This method will clear all selected options. This is only valid when the SELECT supports multiple selections.

Java
C#
Python
Javascript
Copy
WebDriver driver = new ChromeDriver();
driver.get("https://www.testautomationstudio.com/demo/set/");
driver.manage().timeouts().implicitlyWait(Duration.ofSeconds(30));
// Find the select box element
WebElement selectBoxelem = driver.findElement(By.name("cars"));
//Create select box object
Select dropdownObject = new Select(selectBoxelem);
//To deselect all options
dropdownObject.deselectAll();
Copy
IWebDriver driver = new ChromeDriver();
driver.Url = "https://www.testautomationstudio.com/demo/set/";
driver.Manage().Window.Maximize();
driver.Manage().Timeouts().ImplicitWait = TimeSpan.FromSeconds(30);
IWebElement selectBoxelem = driver.FindElement(By.Name("cars"));
//Create select box object
SelectElement dropdownObject = new SelectElement(selectBoxelem);
//To deselect all options
dropdownObject.DeselectAll();
driver.Quit();
Python code coming soon
Javascript code coming soon

deselectByVisibleText

The method deselectByVisibleText will deselect all the option available in the list that displayed with text matching to the given argument. This is only valid when the SELECT supports multiple selections. In the following example we have given argument as Opel, this would select an option having inner text equals to Opel. It will throw NoSuchElementException error, if no matching option elements are found.

Java
C#
Python
Javascript
Copy
WebDriver driver = new ChromeDriver();
driver.get("https://www.testautomationstudio.com/demo/set/");
driver.manage().timeouts().implicitlyWait(Duration.ofSeconds(30));
// Find the select box element
WebElement selectBoxelem = driver.findElement(By.name("cars"));
//Create select box object
Select dropdownObject = new Select(selectBoxelem);
//Select option which is visible in the list
dropdownObject.selectByVisibleText("Opel");
Thread.sleep(2000);
//Deselect option which is visible in the list
dropdownObject.deselectByVisibleText("Opel");
Copy
IWebDriver driver = new ChromeDriver();
driver.Url = "https://www.testautomationstudio.com/demo/set/";
driver.Manage().Window.Maximize();
driver.Manage().Timeouts().ImplicitWait = TimeSpan.FromSeconds(30);
IWebElement selectBoxelem = driver.FindElement(By.Name("cars"));
//Create select box object
SelectElement dropdownObject = new SelectElement(selectBoxelem);
//Select option which is visible in the list
dropdownObject.SelectByText("Opel");
Thread.Sleep(2000);
//Deselect option which is visible in the list
dropdownObject.DeselectByText("Opel");
driver.Quit();
Python code coming soon
Javascript code coming soon

deselectByIndex

The method deselectByVisibleText will deselect the option at the given index. This is done by examining the index attribute of an element. The index starts from 0. This is only valid when the SELECT supports multiple selections. It will throw NoSuchElementException error, if no matching option elements are found.

Java
C#
Python
Javascript
Copy
WebDriver driver = new ChromeDriver();
driver.get("https://www.testautomationstudio.com/demo/set/");
driver.manage().timeouts().implicitlyWait(Duration.ofSeconds(30));
// Find the select box element
WebElement selectBoxelem = driver.findElement(By.name("cars"));
//Create select box object
Select dropdownObject = new Select(selectBoxelem);
//Select option by its index. At index 0 we have Volvo.
dropdownObject.selectByIndex(0);
Thread.sleep(2000);
//Deselect option by its index. At index 0 we have Volvo.
dropdownObject.deselectByIndex(0);
Copy
IWebDriver driver = new ChromeDriver();
driver.Url = "https://www.testautomationstudio.com/demo/set/";
driver.Manage().Window.Maximize();
driver.Manage().Timeouts().ImplicitWait = TimeSpan.FromSeconds(30);
IWebElement selectBoxelem = driver.FindElement(By.Name("cars"));
//Create select box object
SelectElement dropdownObject = new SelectElement(selectBoxelem);
//Select option by its index. At index 0 we have Volvo.
dropdownObject.SelectByIndex(0);
Thread.Sleep(2000);
//Deselect option by its index. At index 0 we have Volvo.
dropdownObject.DeselectByIndex(0);

driver.Quit();
Python code coming soon
Javascript code coming soon

deselectByValue

The method deselectByValue will deselect all the option that have a value matching the given argument. This is only valid when the SELECT supports multiple selections. In the following example we have given argument as saab, this would deselect an option with value equals to saab ie option Saab. It will throw NoSuchElementException error, if no matching option elements are found.

Java
C#
Python
Javascript
Copy
WebDriver driver = new ChromeDriver();
driver.get("https://www.testautomationstudio.com/demo/set/");
driver.manage().timeouts().implicitlyWait(Duration.ofSeconds(30));
// Find the select box element
WebElement selectBoxelem = driver.findElement(By.name("cars"));
//Create select box object
Select dropdownObject = new Select(selectBoxelem);
//Select option by its value
dropdownObject.selectByValue("saab");
Thread.sleep(2000);
//Deselect option by its value
dropdownObject.deselectByValue("saab");
Copy
IWebDriver driver = new ChromeDriver();
driver.Url = "https://www.testautomationstudio.com/demo/set/";
driver.Manage().Window.Maximize();
driver.Manage().Timeouts().ImplicitWait = TimeSpan.FromSeconds(30);
IWebElement selectBoxelem = driver.FindElement(By.Name("cars"));
//Create select box object
SelectElement dropdownObject = new SelectElement(selectBoxelem);
//Select option by its value
dropdownObject.SelectByValue("saab");
Thread.Sleep(2000);
//Deselect option by its value
dropdownObject.DeselectByValue("saab");

driver.Quit();
Python code coming soon
Javascript code coming soon