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

I want to know the the different ways to select an option from a dropdown using Selenium Webdriver?

Hello Dipen, there are a few ways by which you can select an Option from a Dropdown on Webpage using Selenium. The following are a few:

  1. selectByValue(): You can select an option by using the value attribute provided for each option in dropdown menu. So you can use this value to select any particular option:
WebElement element = driver.findElement(By.id("year")); 
Select select = new Select(element); 
select.selectByValue("4"); //

This command will select the year with value 4.

  1. selectByIndex(): Here you can use the index to select the option from a drop down. Basically, Index starts from 0, so this means for the first option, index will be 0 and so on:
WebElement element = driver.findElement(By.id("year")); 
Select select = new Select(element); 
select.selectByIndex("4"); //

This command will select the year with index 4 i.e. 5th option.

  1. selectByVisibleText() : You can also use the visible text to select the option. So if you wants to select “2015”, you can select the option by visible text i.e. 2015:
WebElement element = driver.findElement(By.id("year")); 
Select select = new Select(element); 
select.selectByVisibleText("2015");