Select Class in Selenium | Select Class Methods

Select is a class provided by Selenium that is used to work with a dropdown element.

The select class allows us to select an element from the drop-down and lists that are created with the HTML <select> element.

The select class is present in org.openqa.selenium.support.ui package. It extends Object class and implements the ISelect interface.

The general syntax to declare Select class in Selenium is as follows:

public class Select
  extends java.lang.Object
      implements ISelect, WrapsElement

An HTML <select> tag provides methods and properties for selecting and deselecting options in a dropdown.

Here, we have shown the country dropdown list for demo purposes in the below screenshot.  This kind of country list is generally seen in the registration form of a web application.

Select class in Java

We can also see the basic HTML coding of the dropdown list on the backend side.

<select name="country"> 
<option value="">Country...</option> 
<option value="AF">Afghanistan</option> 
<option value="AL">Albania</option> 
<option value="DZ">Algeria</option> 
<option value="AS">American Samoa</option> 
<option value="AD">Andorra</option> 
<option value="AG">Angola</option> 
<option value="AI">Anguilla</option> 
<option value="AG">Antigua & Barbuda</option> 
<option value="AR">Argentina</option> 
</select>

In the above code, <select> tag is used to create a drop-down list. The <option> tags inside the <select> tag define the available options in the dropdown list that can be selected. The first item in the drop-down list is by default selected.

How to create Object of Select Class in Selenium?


Since the select is an ordinary class, its object is created by using the new keyword. After creating object, we will have to pass the dropdown WebElement as parameter to its constructor.

The syntax to create an object of select class is as follows:

Select select = new Select( WebElement element);

When the element is not a SELECT, UnexpectedTagNameException will throw. Once you are able to create an object of the select class, you can access all the methods provided by the SELECT class by just typing select + dot.

The select + dot (select.) will provide the list of all methods under the Select class. You can choose any method according to the requirement of your test case.


Let’s see a glance at the following source code to look at the list of all methods under select class.
Program source code 1:

package selectClass; 
import org.openqa.selenium.By; 
import org.openqa.selenium.WebDriver; 
import org.openqa.selenium.WebElement; 
import org.openqa.selenium.firefox.FirefoxDriver; 
import org.openqa.selenium.support.ui.Select; 

public class ListofSelectClassMethods 
{ 
 public static void main(String[] args) 
 { 
   WebDriver driver = new FirefoxDriver(); 
   WebElement element = driver.findElement(By.xpath("//select[@name='country']")); 
   Select select = new Select(element); 
   select. 
  } 
}

List of all methods under the select class can be seen in the below screenshot.
List of methods under Select class in Selenium

Now let’s move towards learning about the different types of methods under this Select class.

Select Class Methods in Selenium


The select class provides us multiple methods to select an element from the dropdown list. They are as follows:

selectByValue

This method is used to select an option by passing a value associated with the value attribute. For example, in the previous country list, if we say selectByValue(“AR”), Argentina as a country will get selected.

The general form of this method is as follows:

selectByValue(String value) : void

It takes a String value as a parameter which is one of the values of Select element. It does not return anything.

The syntax to call selectByValue() method is as follows:

select.selectByValue("AR");

For example:

WebElement element = driver.findElement(By.xpath("//select[@name='country']")); 
Select select = new Select(element); 
select.selectByValue("AR"); // Argentina will get selected.

selectByIndex

This method is used to select an option from the dropdown list by passing an index value. The index value ranges from 0 to n – 1 where n is the total number of options in the list.


The general form of this method is as follows:

selectByIndex(int index) : void

It takes the integer value as a parameter which is the index value of Select element. It returns nothing.

The syntax to call selectByIndex() method is as follows:

select.selectByIndex(3); // It will select Algeria.

For example:

WebElement element = driver.findElement(By.xpath("//select[@name='country']")); 
Select select = new Select(element); 
select.selectByIndex(3); // Algeria will get selected.

selectByVisibleText

This is a very easy method to choose an option from any dropdown list. It selects an option by passing the text option that we see on the screen. For example, if we want to select country Albania, we will write selectByVisibleText(“Albania”).

The general form of this method is given below:

selectByVisibleText(String text) : void

It takes a String as a parameter which is one of the text of Select element. This method does not return anything.
The syntax to call selectByVisibleText() method is as follows:

select.selectByVisibleText("Albania");

For example:

WebElement element = driver.findElement(By.xpath("//select[@name='country']")); 
Select select = new Select(element); 
select.selectByVisibleText("Albania"); // Albania will get selected.

deselectAll

deselectAll() method clears all selected entries. It is only valid when the drop-down element list supports multiple selections.

The general syntax to declare deselectAll() method of Java select class is as follows:

deselectAll() : void

It neither accept anything as a parameter nor returns anything.

The syntax to call deselectAll() method is given below:

select.deselectAll();

deselectByIndex

The deselectByIndex() method deselects or clears an option at the specified index. The general syntax for this method is as follows:

  deselectByIndex(int index) : void

It accepts the integer value (index) as a parameter for deselecting the option from the list and returns nothing.

The syntax to call deselectByIndex() method is:

select.deselectByIndex(3);

deselectByValue

This method is used for deselecting all the options whose “value” attribute matches the specified argument. The general syntax for this method is as follows:

deselectByValue(String value) : void

It accepts the string value as a parameter for deselecting the option from the list and returns nothing.

The syntax to call this method with passing value 5 is as:

select.deselectByValue("5"); // It will deselct the option from the list.

deselectByVisibleText

This method is used to deselect all options that display text matching the parameter. The general syntax is as follows:

deselectByVisibleText(String text) : void

It accepts the string text as a parameter for deselecting the option and returns nothing.

The syntax to call this method with passing argument Albania is as follows:

select.deselectByVisibleText("Albania");

getFirstSelectedOption

This method is used to get the first selected option in the list. The general syntax to declare this method is as follows:

getFirstSelectedOption() : WebElement

getFirstSelectedOption() method does not accept anything as a parameter. It returns the first web element from the dropdown list.

For example:

WebElement element = driver.findElement(By.xpath("//select[@name='country']")); 
Select select = new Select(element); 

select.getFirstSelectedOption().getText();
System.out.println(select.getFirstSelectedOption().getText());

getAllSelectedOptions

This method is used to get all selected options in the dropdown list. The general syntax to declare getAllSelectedOptions() method is as follows:

getAllSelectedOptions() : List<WebElement>

It accepts nothing as a parameter and returns the selected options of the list.

isMultiple

This method is used to check that the list supports multiple options at the same time or not. It can be done by checking the value of the “multiple” attribute.

The general syntax to declare isMultiple() method is as:

isMultiple() : boolean

It accepts nothing as a parameter and returns boolean. If the list supports multiple options, it will return true otherwise false returns.


Hope that this tutorial has covered almost all the important points related to Select class in Selenium WebDriver. Keep in mind the following key points related to the select class.


Key Points to Remember

1. Select class in Selenium WebDriver is used to work with dropdown and list elements created with HTML <select> tag.

2. It extends the object class and implements the ISelect interface.

3. The object of select class is created by using new keyword. After creating object, we need to pass dropdown WebElement as a parameter to its constructor.

4. If the element in dropdown is not a SELECT, UnexpectedTagNameException will throw.

5. After creating an object of select class, you can access all the methods provided by SELECT class by just typing select + dot.

Thanks for reading!!!

Next ⇒ How to select dropdown value in Selenium⇐ PrevNext ⇒

Leave a Comment

x