In this tutorial, we are going to study the handling of dropdowns in Selenium WebDriver. For practice, you can check the dummy page having a dropdown element.
Select in Selenium WebDriver
The ‘Select’ class in Selenium WebDriver is used for selecting and deselecting the option in a dropdown. The objects of Select type can be initialized by passing the dropdown webElement as parameter to its constructor.
WebElement testDropDown = driver.findElement(By.id("testingDropdown")); Select dropdown = new Select(testDropDown);
Selecting options from dropdown
There are three ways of selecting options from dropdown-
1. selectByIndex – To select an option based on its index, beginning with 0.
2. selectByValue – To select an option based on its ‘value’ attribute.
3. selectByVisibleText – To select an option based on the text over the option.
Different utility methods in the Select class
- deselectAll() – To deselect all the selected options.
- deselectByIndex(int index) – To deselect the option based on its index.
- deselectByValue(String valueAttribute) – To deselect the option its ‘value’ attribute.
- deselectByVisibleText(String text) – To deselect the option based on the text over the option.
- getOptions() – To return list of all the options(List<WebElement>).
- getAllSelectedOptions() – To return the list of all the selected options(List<WebElement>).
- getFirstSelectedOption() – To return the selected option or the first selected option in case of dropdowns allowing multi-select.
- isMultiple() – To return a boolean value, checking if the dropdown allows multiple option select or not.
That’s all we have in this post. If you have any questions please comment below. Check out the complete tutorial below.
Kuldeep is the founder and lead author of ArtOfTesting. He is skilled in test automation, performance testing, big data, and CI-CD. He brings his decade of experience to his current role where he is dedicated to educating the QA professionals. You can find him on LinkedIn.