How to Select Value from DropDown using Selenium Webdriver

Select Value from DropDown using Selenium Webdriver: In our previous post, we discussed handling checkboxes and radio buttons using the Selenium web driver. In this post, we are trying to learn the rich-featured element and how to select multiple values.

To perform any operation on the dropdown and multiple select Elements, we need to locate that element group because the dropdown is not a single element but a group of elements. There is not much difference between these two elements; the only difference is selecting statements, and you can not select more than one value in a dropdown.

Handle DropDown And Multi-Select List Using Selenium WebDriver

Selecting such elements is like selecting any other elements on a web page. But when doing any operation on those elements, we must import a package “org.openqa.selenium.support.ui.Select,” and create an object of the select class.

Select Class in Selenium WebDriver

This Select class provides many helper methods; with the help of those methods, we can perform multiple operations on the dropdown and multiple select elements. and you need to pass the element details on which you want to perform operations.

Syntax:

WebElement element = driver.findElement(By.id("testing"));
Select oSelect = new Select(element);

Note: Select class only works for elements inside <select> tags.

After creating the object of the select class, you can perform different operations with the object’s help. like below:

package com.selenium.practice.Dropdown;

import java.util.concurrent.TimeUnit;

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.support.ui.Select;

public class DopdownEx 
{
   public static void main(String[] args) throws InterruptedException 
   {

      WebDriver driver;
      System.setProperty("webdriver.chrome.driver","Path Of Browser Driver");

      //When The Checkboxes have an Unique ID
      driver=new ChromeDriver();
      driver.manage().window().maximize();
      driver.get("https://softwaretestingo.blogspot.com/2020/09/dropdown.html");
      driver.manage().timeouts().implicitlyWait(15000, TimeUnit.SECONDS);
      
      WebElement element=driver.findElement(By.id("tools"));
      Select selectElement=new Select(element);
      
      //Select The First Index Element
      selectElement.selectByIndex(1);
      Thread.sleep(3000);
      driver.close();	
   }
}

Select Methods of Select Class

Now, we will learn different methods by which we can select the values from a dropdown or multiple select. Here are some of the methods that we are going to use when selecting the values from a dropdown

  • selectByVisibleText()
  • selectByIndex()
  • selectByValue()
  • getOptions()

Let’s try all the above methods one by one with an example:

Select HTML Dropdown
Select HTML Dropdown

selectByVisibleText(): This is a straightforward option to select a value from a drop-down and multiple select boxes. This method takes a string argument, one of the values of that selected element.

package com.selenium.practice.Dropdown;

import java.util.concurrent.TimeUnit;

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.support.ui.Select;

public class selectByVisibleTextEx 
{
   public static void main(String[] args) throws InterruptedException 
   {

      WebDriver driver;
      System.setProperty("webdriver.chrome.driver","Path Of Browser Driver");

      //When The Checkboxes have an Unique ID
      driver=new ChromeDriver();
      driver.manage().window().maximize();
      driver.get("https://softwaretestingo.blogspot.com/2020/09/dropdown.html");
      driver.manage().timeouts().implicitlyWait(15000, TimeUnit.SECONDS);
      
      WebElement element=driver.findElement(By.id("tools"));
      Select selectElement=new Select(element);
      
      //Select Selenium Using selectByVisibleText Method
      selectElement.selectByVisibleText("Selenium");
      Thread.sleep(3000);
      driver.close();	
   }
}

selectByIndex(): This method also works like selectByVisibleText, but the only difference is that we need to provide the index of the required element here. Based on the index value, it returns the element.

package com.selenium.practice.Dropdown;

import java.util.concurrent.TimeUnit;

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.support.ui.Select;

public class selectByIndexEx 
{
   public static void main(String[] args) throws InterruptedException 
   {

      WebDriver driver;
      System.setProperty("webdriver.chrome.driver","Path Of Browser Driver");

      //When The Checkboxes have an Unique ID
      driver=new ChromeDriver();
      driver.manage().window().maximize();
      driver.get("https://softwaretestingo.blogspot.com/2020/09/dropdown.html");
      driver.manage().timeouts().implicitlyWait(15000, TimeUnit.SECONDS);
      
      WebElement element=driver.findElement(By.id("tools"));
      Select selectElement=new Select(element);
      
      //Select Docker Using Index [Index Always Starts From Zero]
      selectElement.selectByIndex(2);
      Thread.sleep(3000);
      driver.close();	
   }
}

selectByValue(): It’s also similar to selectByVisibleText and selectByIndex, but the difference is it asks for the value described in the DOM inside the value attribute like value=”5″. Based on the value, it returns the element.

package com.selenium.practice.Dropdown;

import java.util.concurrent.TimeUnit;

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.support.ui.Select;

public class selectByValueEx 
{
   public static void main(String[] args) throws InterruptedException 
   {

      WebDriver driver;
      System.setProperty("webdriver.chrome.driver","Path Of Browser Driver");

      //When The Checkboxes have an Unique ID
      driver=new ChromeDriver();
      driver.manage().window().maximize();
      driver.get("https://softwaretestingo.blogspot.com/2020/09/dropdown.html");
      driver.manage().timeouts().implicitlyWait(15000, TimeUnit.SECONDS);
      
      WebElement element=driver.findElement(By.id("tools"));
      Select selectElement=new Select(element);
      
      //Select Cucumber Using Value Attribute
      selectElement.selectByValue("Cucumber");
      Thread.sleep(3000);
      driver.close();	
   }
}

getOptions(): This method gets the option inside the Select tag and returns List<WebElements>.

Select oSelect = new Select(driver.findElement(By.id("yy_date_8")));
List <WebElement> elementCount = oSelect.getOptions();
System.out.println(elementCount.size());
package com.selenium.practice.Dropdown;

import java.util.List;
import java.util.concurrent.TimeUnit;

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.support.ui.Select;

public class getOptionsEx 
{
   public static void main(String[] args) throws InterruptedException 
   {

      WebDriver driver;
      System.setProperty("webdriver.chrome.driver","Path Of Browser Driver");

      //When The Checkboxes have an Unique ID
      driver=new ChromeDriver();
      driver.manage().window().maximize();
      driver.get("https://softwaretestingo.blogspot.com/2020/09/dropdown.html");
      driver.manage().timeouts().implicitlyWait(15000, TimeUnit.SECONDS);
      
      //Locate The Elements
      WebElement element=driver.findElement(By.id("tools"));
      Select selectElement=new Select(element);
      
      //Get The Number Of Values Present Inside Dropdown
      List<WebElement> totalValues=selectElement.getOptions();
      System.out.println(totalValues.size() +" Values are Present Inside The Dropdown");
      Thread.sleep(3000);
      driver.close();	
   }
}

Note: You can use all the above methods in DropDown and Multiple Select Boxes.

isMultiple():

We can use the isMultiple() method to verify whether the text box is supported to select multiple values. When we execute the method, it will return a boolean value; based on that, we can find out. If it returns true, it selects multiple select, and the false value represents that the box does not support the multiple select.

package com.selenium.practice.Dropdown;

import java.util.concurrent.TimeUnit;

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.support.ui.Select;

public class isMultipleEx 
{
   public static void main(String[] args) throws InterruptedException 
   {

      WebDriver driver;
      System.setProperty("webdriver.chrome.driver","Path Of Browser Driver");

      //When The Checkboxes have an Unique ID
      driver=new ChromeDriver();
      driver.manage().window().maximize();
      driver.get("https://softwaretestingo.blogspot.com/2020/09/dropdown.html");
      driver.manage().timeouts().implicitlyWait(15000, TimeUnit.SECONDS);
      
      WebElement element=driver.findElement(By.id("multiselectcars"));
      Select selectElement=new Select(element);
      
      //Find Is that Dropdown Supports Multiple Select Or Not
      //If That Support Multiple Selection Then Returns True else Return False
      Boolean typeOfDropdown=selectElement.isMultiple();
      if(typeOfDropdown)
      {
         System.out.println("This Dropdown Supports Multiple Selection");
      }
      else
         System.out.println("This Dropdown Does Not Support Multiple Selection");
      Thread.sleep(3000);
      driver.close();	
   }
}

Different Deselect methods

We have selected the values using different methods, and in the same way, we can also deselect the selected values. The only difference is that the above-selected methods work for dropdown and multiple select boxes, but the deselect methods only work for multiple boxes.

Deselect Methods Of Selenium Select Class
Deselect Methods Of Selenium Select Class

Here are the available methods:

  • deselectAll(): It helps you to deselect all selected values.
  • deselectByIndex(): It will deselect the given index value only.
  • deselectByValue(): It will deselect the value of the matching argument.
  • deselectByVisibleText(): Deselect all the values that display the matching text.

deselectAll() Example:

package com.selenium.practice.Dropdown;

import java.util.concurrent.TimeUnit;

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.support.ui.Select;

public class deselectAllEx 
{
   public static void main(String[] args) throws InterruptedException 
   {

      WebDriver driver;
      System.setProperty("webdriver.chrome.driver","Path Of Browser Driver");

      //When The Checkboxes have an Unique ID
      driver=new ChromeDriver();
      driver.manage().window().maximize();
      driver.get("https://softwaretestingo.blogspot.com/2020/09/dropdown.html");
      driver.manage().timeouts().implicitlyWait(15000, TimeUnit.SECONDS);
      
      WebElement element=driver.findElement(By.id("multiselectcars"));
      Select selectElement=new Select(element);
      
      //Deselect All The Selected Values
      selectElement.selectByIndex(2);
      Thread.sleep(3000);
      selectElement.deselectAll();
      Thread.sleep(3000);
      driver.close();	
   }
}

deselectByIndex Example:

package com.selenium.practice.Dropdown;

import java.util.concurrent.TimeUnit;

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.support.ui.Select;

public class deselectByIndexEx 
{
   public static void main(String[] args) throws InterruptedException 
   {

      WebDriver driver;
      System.setProperty("webdriver.chrome.driver","Path Of Browser Driver");

      //When The Checkboxes have an Unique ID
      driver=new ChromeDriver();
      driver.manage().window().maximize();
      driver.get("https://softwaretestingo.blogspot.com/2020/09/dropdown.html");
      driver.manage().timeouts().implicitlyWait(15000, TimeUnit.SECONDS);
      
      WebElement element=driver.findElement(By.id("multiselectcars"));
      Select selectElement=new Select(element);
      
      //Select Docker Using Index [Index Always Starts From Zero]
      selectElement.selectByIndex(1);
      Thread.sleep(3000);
      selectElement.deselectByIndex(1);
      driver.close();	
   }
}

deselectByValue Example:

package com.selenium.practice.Dropdown;

import java.util.concurrent.TimeUnit;

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.support.ui.Select;

public class deselectByValueEx 
{
   public static void main(String[] args) throws InterruptedException 
   {

      WebDriver driver;
      System.setProperty("webdriver.chrome.driver","Path Of Browser Driver");

      //When The Checkboxes have an Unique ID
      driver=new ChromeDriver();
      driver.manage().window().maximize();
      driver.get("https://softwaretestingo.blogspot.com/2020/09/dropdown.html");
      driver.manage().timeouts().implicitlyWait(15000, TimeUnit.SECONDS);
      
      WebElement element=driver.findElement(By.id("multiselectcars"));
      Select selectElement=new Select(element);
      
      //Deselect Audi Using deselectByValue Value Attribute
      selectElement.selectByValue("audi");
      Thread.sleep(3000);
      selectElement.deselectByValue("audi");
      Thread.sleep(3000);
      driver.close();	
   }
}

deselectByVisibleText Example:

package com.selenium.practice.Dropdown;

import java.util.concurrent.TimeUnit;

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.support.ui.Select;

public class deselectByVisibleTextEx 
{
   public static void main(String[] args) throws InterruptedException 
   {

      WebDriver driver;
      System.setProperty("webdriver.chrome.driver","Path Of Browser Driver");

      //When The Checkboxes have an Unique ID
      driver=new ChromeDriver();
      driver.manage().window().maximize();
      driver.get("https://softwaretestingo.blogspot.com/2020/09/dropdown.html");
      driver.manage().timeouts().implicitlyWait(15000, TimeUnit.SECONDS);
      
      WebElement element=driver.findElement(By.id("multiselectcars"));
      Select selectElement=new Select(element);
      
      //Desklect Audi Using deselectByVisibleText
      selectElement.selectByVisibleText("Audi");
      Thread.sleep(3000);
      selectElement.deselectByVisibleText("Audi");
      Thread.sleep(3000);
      driver.close();	
   }
}

Note:

If you are trying to deselect a dropdown that does not support multiple selections, you will get UnsupportedOperationException with the below message in your Editor console.

Exception in thread “main” java.lang.UnsupportedOperationException: You may only deselect options of a multi-select.

If you are trying to locate the element using selectByValue() or deselectByValue(), But if you give the wrong value, you will get java.net.SocketTimeoutException.

I love open-source technologies and am very passionate about software development. I like to share my knowledge with others, especially on technology that's why I have given all the examples as simple as possible to understand for beginners. All the code posted on my blog is developed, compiled, and tested in my development environment. If you find any mistakes or bugs, Please drop an email to softwaretestingo.com@gmail.com, or You can join me on Linkedin.

Leave a Comment