What are the different ways to select an option from a dropdown using Selenium Webdriver?

We have different ways to select an option from a dropdown using Selenium webdriver. This is done with the help of the Select class. A dropdown in the html code is represented by select tag.
The options in a dropdown are represented by the option tag. Also, we have to add the statement org.openqa.selenium.support.ui.Select to the code to work with dropdown.
The different methods to select an option from a dropdown are listed below −
selectByIndex – index of the option to be selected by the dropdown is passed as a parameter. The index starts from 0.
selectByIndex – index of the option to be selected by the dropdown is passed as a parameter. The index starts from 0.
WebElement e = driver.findElement(By.className("opt"));
Select s = new Select(e);
s.selectByIndex(0);
selectByValue – value attribute of the option to be selected by the dropdown is passed as a parameter. The options in the dropdown should have the value attribute so that this method can be used.
selectByValue – value attribute of the option to be selected by the dropdown is passed as a parameter. The options in the dropdown should have the value attribute so that this method can be used.
WebElement e = driver.findElement(By.className("opt"));
Select s = new Select(e);
s.selectByIndex("option 1");
selectByVisibleText – visible text of the option to be selected by the dropdown is passed as a parameter.
selectByVisibleText – visible text of the option to be selected by the dropdown is passed as a parameter.
WebElement e = driver.findElement(By.className("opt"));
Select s = new Select(e);
s.selectByVisibleText("Selenium");
Let us try to select an option from the below dropdown −
Code Implementation with selectByIndex
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 org.openqa.selenium.support.ui.Select
public class DrpSelectByIndex{
   public static void main(String[] args) {
      System.setProperty("webdriver.gecko.driver",
         "C:\\Users\\ghs6kor\\Desktop\\Java\\geckodriver.exe");
      WebDriver driver = new FirefoxDriver();
      //implicit wait
      driver.manage().timeouts().implicitlyWait(5, TimeUnit.SECONDS);
      //URL launch
      driver.get("https://www.tutorialspoint.com/tutor_connect/index.php/");
      //identify dropdown
      WebElement n = driver.findElement(By.name("selType"));
      //select option by index
      Select s = new Select(n);
      s.selectByIndex(0);
      driver.quit();
   }
}
Code Implementation with selectByValue
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 org.openqa.selenium.support.ui.Select
public class DrpSelectByValue{
   public static void main(String[] args) {
      System.setProperty("webdriver.gecko.driver",
         "C:\\Users\\ghs6kor\\Desktop\\Java\\geckodriver.exe");
      WebDriver driver = new FirefoxDriver();
      //implicit wait
      driver.manage().timeouts().implicitlyWait(5, TimeUnit.SECONDS);
      //URL launch
      driver.get("https://www.tutorialspoint.com/tutor_connect/index.php/");
      //identify dropdown
      WebElement n = driver.findElement(By.name("selType"));
      //select option by value attribute of option
      Select s = new Select(n);
      s.selectByValue("subject");
      driver.quit();
   }
}
Code Implementation with selectByVisibleText
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 org.openqa.selenium.support.ui.Select
public class DrpSelectByVisibleTxt{
   public static void main(String[] args) {
      System.setProperty("webdriver.gecko.driver",
         "C:\\Users\\ghs6kor\\Desktop\\Java\\geckodriver.exe");
      WebDriver driver = new FirefoxDriver();
      //implicit wait
      driver.manage().timeouts().implicitlyWait(5, TimeUnit.SECONDS);
      //URL launch
      driver.get("https://www.tutorialspoint.com/tutor_connect/index.php/");
      //identify dropdown
      WebElement n = driver.findElement(By.name("selType"));
      //select option by value attribute of option
      Select s = new Select(n);
      s.selectByVisibleText("By Name");
      driver.quit();
   }
}