Test Diary


Social Profile

Recent Comments

My Third Selenium WebDriver Test (drop downs)

Olufemi Ade-OlusileOlufemi Ade-Olusile

Menu Next Topic

In our previous posts we learnt about inserting text into a text field, navigating (pages) to a URL and different Selenium waits . Moving ahead, we will take a look on how we can handle drop downs with Selenium. 

For this tutorial we will be using our sample test page.

Drop Downs

On our test page we have two drop downs. To get this locator we need to right click on a drop down (Blue Shirt) and inspect element. This will enable you view the html code for that element as displayed in the below.

If you look closely at the html, you will notice that our drop down has an id = “Shirts”. Also we have all the values below that id. Selenium provides us with different ways to select from a drop down.

  1. Select by visible text 
    new Select(driver.findElement(By.id("Shirts"))).selectByVisibleText("Red Shirt");

    We have selected by visible text, which means we can select any of the options in the drop down by using the text visible to us. “Blue Shirt”, “Red Shirt”, “Yellow Shirt”. Remember that Java is case sensitive, so you must insert your text with the right case.

  2. Select by value 
    new Select(driver.findElement(By.id("Shirts"))).selectByValue("red");

     Every select option in the drop down has a value set to it. We can use this value to select from the drop down “blue” “red” “yellow”. 

  3. Select by index 
    new Select(driver.findElement(By.id("Shirts"))).selectByIndex(1);

    You may wonder what we mean by index? This just means we can count the number of values and use numbers (index) to parameterize them. 0 = “Blue Shirt”, 1 = “Red Shirt”, 2 = “Yellow Shirt”. 

Now lets use this in our test script.

Third Test (drop downs) 
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.Select;
import org.openqa.selenium.support.ui.WebDriverWait;
public class TestdiaryDropDowns {

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

WebDriver driver = new FirefoxDriver();


String contactUrl = "http://www.testdiary.com/training/selenium/selenium-test-page/";


(new WebDriverWait(driver, 10))

// Select from the drop down by visible text
new Select(driver.findElement(By.id("Shirts")))
.selectByVisibleText("Red Shirt");

// we are using this thread.sleep, so you can verify that Selenium picks
// Red Shirt from the
// drop down

// Select from the drop down by value
new Select(driver.findElement(By.id("Skirts")))

// we are using this thread.sleep, so you can verify that Selenium picks
// Yellow Skirt from the
// drop down

WebElement name = driver.findElement(By.name("your-name"));



WebElement email = driver.findElement(By.name("your-email"));



WebElement subject = driver.findElement(By.name("your-subject"));
subject.sendKeys("New tester");

WebElement your_message = driver.findElement(By.name("your-message"));
your_message.sendKeys("I want to be the best tester.");

WebElement send = driver


String expected_message = "Your message was sent successfully. Thanks.";

(new WebDriverWait(driver, 10))

String actual_message = driver.findElement(

if (expected_message.equals(actual_message)) {

System.out.println("verification successful - " + actual_message);
} else {
System.out.println("verification unsuccessful");



In conclusion, we have looked at different ways to select from a drop down and how they can be used effectively in our test scripts.You can download or clone this test from the Testdiary Github.

Click: drop downs Happy Testing!!!

Menu Next Topic

“My name is Olufemi Ade-Olusile, and I am a Software Developer in Test. I am the owner of "Test diary" which is aimed at inspiring and teaching software testers.