Writing Automation Script for List box

Writing Automation Script for List box

In this tutorial, we are going to learn about writing automation script for List box in Selenium 3.0. List boxes are designed to allow selection of multiple options unlike drop down list box & radio buttons.

Objective of this tutorial is to demonstrate:

  • How to write test cases for unit testing of List boxes?
  • What are key usage scenario for multi-select list boxes

Web page used for this Tutorial

We are going to use https://techcanvass.com/Examples/multi-select.html for this example. This is a simple web page with multi-select list box.

Multi Select List Box

There are two ways in which we can select the desired value from a multi-select list box as listed below:

  • selectByValue()
  • selectByIndex()

Method I: using selectByValue()

As discussed previously, we need to instantiate the multi-select list box before selecting a particular value. As an example, let’s select the value Opel from the multi-select list box.

Selecting Value from List Box

ID of the multi-select box is “multiselect” and we will use it to instantiate the list box.

Select listbox = new Select(driver.findElement(By.id(“multiselect”)));

We then use selectByValue() method to select a particular value from the multi-select list box. The code is as shown below:


Since we can select multiple values from the list box, we can simple calling selectByValue() multiple times.

Method II: using selectByIndex()

The selecyByIndex() works almost in the similar way like selectByValue(). The difference is that it uses index of the value to be selected. Using the same example as above, we will select “opel” and “audi” values from the multi-select list box.

The index starts from 0 in multi-select list box. This is true for even drop down list box as indicated in the previous article:

Read Also: Writing automation script for Dropdown list box

We can select multiple values using index values as shown below:

Select listbox = new Select(driver.findElement(By.id(“multiselect”)));

You can also select multiple values by using the method selectByIndex() multiple times.

Finding the selected value in the multi-select list box

How can you find the selected value in the multi-select list box ?

We have a getFirstSelectedOption() method in selenium for getting the selected value from the drop down list box. This method returns a WebElement object. The code below shows the usage:

WebElement option = listbox.getFirstSelectedOption();

We can also print the selected value by using getText() method as shown below:

System.out.println(option.getText()); //prints selected option

As multi-select list box has the multi-select feature, we can use another method to print all the selected options as shown below:

//Listing down all the selected options
List<WebElement> options = listbox.getAllSelectedOptions();
for (WebElement option2 : options) {
System.out.println(option2.getText()); //Prints “Option”, followed by “Not Option”

Listing down all the option

We can list down all the options or values of the multi-select list box by using the following piece of code:

//Listing down all the options
List<WebElement> options = listbox.getOptions();
for (WebElement option : options) {

We have covered the most important methods for the usage in the case of a multi-select list box. You can explore other methods.

Complete code

We present the complete code for selecting multiple options (“opel” and “audi”) and printing them.

package basicSeleniumScripts;

import java.util.List;

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 MultiselectListBox {

public static void main(String[] args) throws InterruptedException {

System.setProperty(“webdriver.chrome.driver”, “E:\\Selenium 3\\chromedriver.exe”);
WebDriver driver;
driver = new ChromeDriver();

Select listbox = new Select(driver.findElement(By.id(“multiselect”)));

//prints first option which is selected
WebElement option = listbox.getFirstSelectedOption();

System.out.println(“—Showing all the selected options—-“);

//Listing down all the selected options
List<WebElement> options = listbox.getAllSelectedOptions();
for (WebElement option2 : options) {
System.out.println(option2.getText()); //Prints “Option”, followed by “Not Option”

//Quitting the web driver



What’s Next?

In this tutorial, we explained how to write scripts for testing a list box. You can go and read more Selenium tutorials published on this blog. You can also go and check out the new look official Selenium Website.

Are you looking to become an Automation tester and learn Selenium?

Techcanvass offers Automation Testing and Business Analysis certification courses. We are an IIBA endorsed education provider (EEP), iSQI Germany Authorized Training as well as Agile Testing alliance partner for CP-SAT certification training in Selenium.

One of the key features of our courses is the Job readiness package, as shown below:

Online Selenium Training
Selenium Certification Training