Select Class in Selenium : How to select a value in dropdown list?
Selenium is the most widely used tool for end-to-end automation testing of a website. This article discusses how Selenium is used to find web elements in a drop-down menu on a website.
Selenium Select class provides the implementation of the HTML SELECT tag. A Select tag provides the helper methods with select and deselect options. As Select is an ordinary class, its object is created by the keyword New and also specifies the location of the web element.
Syntax:
Select objSelect = new Select();
In the syntax above, it clearly states that Select is asking for an element type object for its constructor, i.e it will create an object of the select class.
Different Select Methods
1. selectByVisibleText: selectByVisibleText(String arg0): void
This method is used to select one of the options in a drop-down box or an option among multiple selection boxes.
Syntax:
obj.Select.selectByVisibleText(“text”);
2. selectByIndex: selectByIndex(int arg0) : void
This method is similar to ‘selectByVisibleText’, but the difference here is that the user has to provide the index number for the option rather than text.
Syntax:
oSelect.selectByIndex(int);
3. selectByValue: selectByValue(String arg0) : void
This method asks for the value of the desired option rather than the option text or an index.
Syntax:
oSelect.selectByValue(“text”);
4. getOptions: getOptions( ) : List<WebElement>
This method gets all the options belonging to the Select tag. It takes no parameter and returns List<WebElements>.
Syntax:
oSelect.getOptions();
5. deselectAll()
This method clears all the selected entries.
Syntax:
objSelect.deselectAll();