Handling Dropdowns in Selenium Webdriver using Select Method

Before starting lets first create a demo page to work with.
 
HTML Code for demo page
  1. <html>    
  2.    <head>    
  3.       <title>Multi select Drop Down List Box</title>    
  4.    </head>    
  5.    <body>    
  6.       <div align="center">    
  7.          <p>Which device do you own?</p>    
  8.          <p>Please select it using the below dropdown</p>    
  9.          <select name="devices">    
  10.             <option selected> Please select</option>    
  11.             <option value="iphone">iPhone</option>    
  12.             <option value="lenovo">Lenovo</option>    
  13.             <option value="samsung">Samsung</option>    
  14.             <option value="xiaomi">Xiaomi</option>    
  15.             <option value="windows">Windows Phone</option>    
  16.          </select>    
  17.       </div>    
  18.    </body>    
  19. </html>   
 
 
'Select' class provides us some useful methods to interact with dropdowns to select dropdown option using below methods.
  • selectByIndex
  • selectByValue
  • selectByVisibleText
selectByIndex: This method will select the dropdown option based on the dropdown index.
  1. public class HandleDropDown {    
  2.         
  3.     public void selectByIndexExample(WebDriver driver){    
  4.         WebElement element=driver.findElement(By.name("devices"));    
  5.         Select se=new Select(element);    
  6.         se.selectByIndex(1);  //this will select iPhone    
  7.     }    
  8.         
  9.     public static void main(String[] args) throws InterruptedException {    
  10.         WebDriver driver=new FirefoxDriver();    
  11.         driver.get("D:\\dp.html");    
  12.         HandleDropDown dp=new HandleDropDown();    
  13.         dp.selectByIndexExample(driver);    
  14.     }    
  15. }   
  
selectByValue: This method will select the dropdown option based on the value matching with the given argument.
  1. public class HandleDropDown {    
  2.         
  3.     public void selectByValueExample(WebDriver driver){    
  4.         WebElement element=driver.findElement(By.name("devices"));    
  5.         Select se=new Select(element);    
  6.         se.selectByValue("lenovo"); //This will select Lenovo    
  7.     }    
  8.         
  9.     public static void main(String[] args) throws InterruptedException {    
  10.         WebDriver driver=new FirefoxDriver();    
  11.         driver.get("D:\\dp.html");    
  12.         HandleDropDown dp=new HandleDropDown();    
  13.         dp.selectByValueExample(driver);    
  14.     }    
  15. }    
  
selectByVisibleText: This method will select the dropdown option based on displaying text in the dropdown. 
  1. public class HandleDropDown {    
  2.             
  3.     public void selectByVisibletextExample(WebDriver driver){    
  4.         WebElement element=driver.findElement(By.name("devices"));    
  5.         Select se=new Select(element);    
  6.         se.selectByVisibleText("Windows Phone");  //This will select Windows Phone  
  7.     }    
  8.         
  9.     public static void main(String[] args) throws InterruptedException {    
  10.         WebDriver driver=new FirefoxDriver();    
  11.         driver.get("D:\\dp.html");    
  12.         HandleDropDown dp=new HandleDropDown();    
  13.         dp.selectByVisibletextExample(driver);    
  14.     }    
  15. }    
 
Next Recommended Reading Allure Report In Webdriver IO