Hello Folks,
In current series of posts we have learnt:
Now, we will see how to handle multi select drop-down using Select class.
We will be using below html code of multi select drop-down:
When you will open above html file in a browser, you see as below:
Let’s see frequent operations performed on multi select drop-down:
How to check if any drop-down is single select or multi select:
- Select class provides a method named isMultiple() which returns a boolean.
- If drop-down is multi select, isMultiple() will return true otherwise false.
Code:
Output:
Drop-down is multi select.
How to print all options available in drop-down:
- Printing all options available in a drop-down is same for single select and multi select drop-down. We need to use getOptions() method of Select class.
- Select class provides a method named getOptions() to get all available options in a drop-down.
- Return type of getOptions() method is a List<WebElement>. Reason of returning List is a drop-down may have duplicate values as well. So to retrieve all options, selenium developers use List as a return type of getOptions() method.
- After getting List<WebElement>, you need to iterate elements of List and use getText() method to get option value.
Code:
Output:
All options are printed below: Select Hindi English Kannada Tamil Telgu Malaylam
How to select values from multi select drop-down:
- Method used for selecting values from multi select drop-down are same as for single select drop-down.
- In single selected drop-down, only one value will be selected but in multi select drop-down, whenever you will call selectByXXXX(), new options will be selected and previously will not be affected.
- We can select a value from drop-down using three ways:
- Using index. (Index starts from zero.).
- Using value.
- Using visible text.
If you see above html code, every option has default index value which is order of their occurrence. For example: First OPTION tag has index zero , second OPTION tag has index one and so on.
- If you see above html code carefully, you will find every OPTION tag has a value attribute. We can use this also to select from a drop-down. Value can be any thing. For example: Value of OPTION tag which has inner text as Hindi can be anything other than Hindi also.
<option value=”Mother Tongue”>Hindi</option>
- Inner text of OPTION tag is called as visible text. This can also be used to select value from drop-down.
We will see a sample program here. We will select two languages : Hindi and Kannada.
Code:
Output:
You should notice, two options are selected.
How to get all selected options in a multi drop-down:
- Select class provides a method named getAllSelectedOptions() which returns a List<WebElement>.
- You need to call getText() method of web elements of List to get selected options.
Code:
Output:
Selected options are as below: Hindi Kannada
How to deselect all values from drop-down:
- Select class provides a method named deselectAll() which will deselect all selected options.
- Remember , this method is only for multi select dropdown. You can not use with single select drop-down. If you use with single select drop-down, you will get exception java.lang.UnsupportedOperationException.
Code:
selectObject.deselectAll();
How to deselect options one by one:
- Select class provides three methods to deselect option.
- public void deselectByIndex(int index)
- public void deselectByValue(java.lang.String value)
- public void deselectByVisibleText(java.lang.String text)
- Remember, above methods are used only for multi select dropdown. You can not use with single select drop-down. If you use with single select drop-down, you will get exception java.lang.UnsupportedOperationException.
- Suppose you want to deselect an option which you never selected, you will not get any exception. Above methods will check if option is selected then it will be deselected otherwise no other action will be performed.
Code:
selectObject.deselectByIndex(1); selectObject.deselectByValue("Kannada");
Complete Java code:
That’s all for multiselect drop-down operations. If you want to know for any other operation on mutli select drop-down, please let me know. I will add in this post.
If you have any doubt, feel free to ask here.
If you like my posts, please like, comment, share and subscribe.
#ThanksForReading
#HappySelenium