As many of you will be aware, they are numerous controls out there on the internet these days. By controls, I mean things like Select Elements, Radio Buttons, Mutli-Select Elements and much many more. Far too many for WebDriver to offer easy support for. However, they have made it easy for us to interact with Select elements. Or Dropdowns as I prefer to call them.
They’ve made it easy for us by creating a class representation of a drop-down. We can initiate one like this
Select selGender = new Select(driver.findElement(By.id("gender")));
The object created for us by Selenium is called Select. So we’re making our own instance of it and calling it selGender. This is my preferred naming convention.
We then instruct Java to create a new Select object. You’ll see that the Select object takes a parameter, that parameter is WebElement. We’ve covered those in the Locator lessons.
So we need to tell WebDriver how to find our Select element on the page. In the instance above I’m using an ID. But you can use any of the locator strategies we covered in the Locators lesson.
We can now take advantage of all the methods the Selenium team have created for us.
This will return the first selected option in the drop-down. In most cases, your select is probably single select, so this will return the selected option to you.
If you’re not familiar with Selects in HTML, they have two values. The first value tends to be used by the application, and the second value is what the user sees on the screen. A common pattern and a good example of this is language drop-downs. The value will be something like ‘en-GB’ but the text will be ‘British’.
<select id="language" class="gender">
<option value="en-us">American English</option>
So in this code example we could select British by using the following code
This is the most common method I use to select values though, as this is what the user will be seeing.
Take the same code above, we could select British by using
You can instruct WebDriver to select the 2nd item in the Dropbox. Could be useful for checking the order of values in the drop-down.
This is a nice method. It will return you a list of all the options in the drop-down. Again could for testing all the options that should be there, are indeed there.
It returns you a list of WebElements, so behaves the same as FindElements() from our lesson on Locators.
List<WebElements> genderOptions = selGender.getOptions();
Behaves exactly like the method above, but if the Select was a multi-select it would return you all the selected options.
This will return you a True or False, based on whether the dropdown is multi-select or not.
This will do what it says, I’ve only ever needed to use this on multi-selects.
This again will do what they same, follow the same pattern as the select examples above.