How to display all items in the list in the drop down in Selenium?

We can display all items in the list in the dropdown with Selenium webdriver using the Select class. A dropdown is represented by select tag and itsoptions are represented by option tag.

To obtain all the list of items we have to use the method getOptions. Its return type is list. Then we have to iterate through this list and obtain it with the help of the getText method.

Let us see the html code of a dropdown along with its options – Please select an option, Option 1 and Option 2.


WebElement d = driver.findElement(By.tagName("select"));
Select l = new Select(d);
List<WebElement> m = l.getOptions();


import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.firefox.FirefoxDriver;
import java.util.concurrent.TimeUnit;
import java.util.List;
public class DrpdwnLst{
   public static void main(String[] args) {
      WebDriver driver = new FirefoxDriver();
      //implicit wait
      driver.manage().timeouts().implicitlyWait(5, TimeUnit.SECONDS);
      //URL launch
      // identify dropdown
      WebElement d = driver.findElement(By.tagName("select"));
      //Select class to get options in dropdown
      Select l = new Select(d);
      List<WebElement> m = l.getOptions();
      System.out.println("Drodown list items are: ");
      //iterate through options till list size
      for (int j = 0; j < m.size(); j++) {
         String s = m.get(j).getText();