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
Kotlin
Copy
WebDriver driver = new ChromeDriver();
driver.get("http://localhost:8080/demo/getData/");
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();

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
Kotlin
Copy
WebDriver driver = new ChromeDriver();
driver.get("http://localhost/demo/setData/");
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");
driver.quit();

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
Kotlin
Copy
WebDriver driver = new ChromeDriver();
driver.get("http://localhost:8080/demo/setData/");
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);
driver.quit();

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
Kotlin
Copy
WebDriver driver = new ChromeDriver();
driver.get("http://localhost:8080/demo/setData/");
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");
driver.quit();

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
Kotlin
Copy
WebDriver driver = new ChromeDriver();
driver.get("http://localhost:8080/demo/setData/");
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());
}
driver.quit();

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
Kotlin
Copy
WebDriver driver = new ChromeDriver();
driver.get("http://localhost:8080/demo/setData/");
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());
}

driver.quit()

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
Kotlin
Copy
WebDriver driver = new ChromeDriver();
driver.get("http://localhost/demo/setData/");
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());
driver.quit()

deselectAll

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

Java
C#
Python
Javascript
Kotlin
Copy
WebDriver driver = new ChromeDriver();
driver.get("http://localhost:8080/demo/setData/");
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();
driver.quit();

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
Kotlin
Copy
WebDriver driver = new ChromeDriver();
driver.get("http://localhost:8080/demo/setData/");
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");
driver.quit();

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
Kotlin
Copy
WebDriver driver = new ChromeDriver();
driver.get("http://localhost:8080/demo/setData/");
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);
driver.quit()

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
Kotlin
Copy
WebDriver driver = new ChromeDriver();
driver.get("http://localhost:8080/demo/setData/");
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");
driver.quit();