In this article we learn the how to handle dropdown in selenium, what is select tag and how to select and deselect values from dropdown.
How to Handle Dropdown in Selenium Webdriver?
What is <select> tag?
The <select> element, used along with one or more <option> elements, creates a drop-down list of options for a web form. The <select> element creates the list and each <option> element is displayed as an available option in the list.
Dropdown can be of two types:
1. Single select drop-down: We can select only single value at a time.
2. Multi select drop-down: We can select more than one value at a time.
Handling <select> tag dropdown in Selenium:
1. Selenium developers provide a special class called “Select” to handle dropdown.
2. “Select” class is present in “org. openqa. selenium. support.ui” package.
3. “Select” class models a SELECT tag and provides helper methods to select and deselect options.
4. “Select” class can be used only for SELECT tag dropdown.
5. What happens when we try to use “Select” class for non-SELECT tag dropdowns. “Select” class provides an argument constructor which accepts a web element which should be a <select> tag web element. This constructor checks that the given web element is a SELECT tag or not. If it is not, then an UnexpectedTagNameException is thrown
How to check if any dropdown is Single select or Multi select:
1. Select class provides a method named isMultiple () which returns a boolean.
2. If dropdown is multi select, isMultiple () will return true otherwise false.
// To check if drop-down is multi select
boolean status= selectObject.isMultiple ();
System.out.println(“Dropdown is multi select.”);
System.out.println(“Dropdown is single select”);
Dropdown is multi select
How to print all options available in dropdown:
1. Printing all options available in a dropdown is same for single select and multi select dropdown. We need to use getOptions () method of Select class.
2. Select class provides a method named getOptions () to get all available options in a dropdown.
3. Return type of getOptions () method is a List<WebElement>. Reason of returning List is a dropdown may have duplicate values as well. So, to retrieve all options, selenium developers use List as a return type of getOptions () method.
4. After getting List<WebElement>, we need to iterate elements of List and use getText () method to get option value.
How to select values from multi select dropdown:
1. Method used for selecting values from multi select drop-down are same as for single select dropdown.
2. In single selected dropdown, only one value will be selected but in multi select dropdown, whenever you will call selectBy(), new options will be selected and previously will not be affected.
3. We can select a value from dropdown using three ways:
i) selectByVisibleText ();
ii) selectByIndex ();
iii) selectByValue ();
How to get all selected options in a multi dropdown:
1. Select class provides a method named getAllSelectedOptions () which returns a List<WebElement>.
2. We need to call getText () method of web elements of List to get selected options
How to deselect all values from drop-down:
1. Select class provides a method named deselect All () which will deselect all selected options.
2. Note- This method is only for multi select dropdown. We cannot use with single select drop-down. If we use with single select dropdown, we will get exception java.lang.UnsupportedOperationException.
How to deselect options one by one:
1. Select class provides three methods to deselect option.
i) deselectByVisibleText ();
ii) deselectByIndex ();
iii) deselectByValue ();
2. Above methods are used only for multi select dropdown. You we not use with single select drop-down. If you use with single select drop-down, you will get exception java. lang. UnsupportedOperationException.
3. Click on drop down and iterate through options.
4. Click on drop down to make options visible.
5. Get all options of drop down.
6. Iterate through it and match with option to be selected.
7. If found then click on option for selection.
8. Optimal logic to select value in drop down without iteration
9. Click on drop down to make options visible.
10. Create a custom locator using option to be selected.
11. Locate it and select.