How to get all the options in the dropdown in Selenium?


We can extract all the options in a dropdown in Selenium with the help of Select class which has the getOptions() method. This retrieves all the options on a Select tag and returns a list of web elements. This method does not accept any arguments.

Example

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import java.util.concurrent.TimeUnit;
import java.util.List;
import org.openqa.selenium.support.ui.Select;
public class DropdownOptions{
   public static void main(String[] args) {
      System.setProperty("webdriver.chrome.driver", "C:\Users\ghs6kor\Desktop\Java\chromedriver.exe");
      WebDriver driver = new ChromeDriver();
      String url = " https://www.tutorialspoint.com/tutor_connect/index.php";
      driver.get(url);
      driver.manage().timeouts().implicitlyWait(12, TimeUnit.SECONDS);
      Select s = new Select(driver.findElement(By.xpath("//select[@name=’selType’]")));
      // getting the list of options in the dropdown with getOptions()
      List <WebElement> op = s.getOptions();
      int size = op.size();
      for(int i =0; i<size ; i++){
         String options = op.get(i).getText();
         System.out.println(options);
      }
      driver.quit()
   }
}

Updated on: 11-Jun-2020

17K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements