Special Offer - Enroll Now and Get 2 Course at ₹25000/- Only Explore Now!

All Courses
Select Class in Selenium

Select Class in Selenium

January 17th, 2020

Select Class in Selenium

There are different types of WebObjects that can be embedded in our webpage. Each has its own purpose, Likewise DropDown is one among them. It restricts the user to select the required option and also provide the user the convenience to select the option without the need to type it manually. Let’s start to create a DropDown value on our webpage.
Sample Dropdown
Sample Dropdown Output

Source Code for the above DropDown would be,
<!DOCTYPE html>
<div>Sample DropDown</div>
<option value="Besant">Besant</option>
<option value="Smp">samp</option>
<option value="Driver">Driver</option>
<option value="Course">course</option>
We can automate the above Element using Select Class
WebElement element=null;
Select besantSele =new Select(element);
Element Using Select Class If we need to use select class we must have the below in our class/project
import org.openqa.selenium.support.ui.Select;
Then we can use the reference variable to make our action.


This is very much self-explanatory, as the name suggests we can make our object to select the dropdown value based on our Visible text on our browser.


This would select the value of the dropdown based on the index value starting from zero


In our previous source code, we have added Value attribute along with string. We can also select the required value based on it.
Above all the methods would not return value.
There can be a scenario where we need to check the values inside the dropdown, In such cases, we don’t have any built-in method but we can extract all the options available in the DropDown and then we can iterate to check it.

Select obj= new Select(driver.findElement(By.id("678gjhb")));
List<WebElement> elementlist = obj.getOptions();
There is a method available in selenium class to get all the options stored in a variable “elementlist”. Using this variable we can perform the required operation on it. Moving ahead, there can be a scenario where we need to select multiple values. Manually, when we need to do this we can try holding the “CTRL” key and select the options. This can be achieved by using isMultiple() which returns a Boolean value. This can be implemented in the below way,
Select obj= new Select(driver.findElement(By.id("sdf343")));
Similarly, we can deselect the required values by using the below available methods.

  • deselect()
  • deselectByIndex(int)
  • deselectByValue(string)
  • deselectByVisibletext(string)

These are not much-complicated methods. These are more self-explanatory and we can use the same way as we used the previous methods.

Related Blogs