Select Value from DropDown using Selenium Webdriver: In our previous post, we have discussed how yo can handle checkboxes and radio button using selenium web driver. This post, we are trying to learn rich-featured element Drop down and how can we select multiple values in that.
To perform any operation on the dropdown and multiple select Element, we need to locate that element group because dropdown is not a single element its a group of elements. There is not much difference between these two elements, the only difference is selecting statements, and you can not select more than one value in a dropdown.
Handle DropDown And Multi-Select List Using Selenium WebDriver
To select such elements is like selecting any other elements on a web page. But when it comes to doing any operation on those elements, we need to import a package “org.openqa.selenium.support.ui.Select,” and we need to create an object of the select class.
Select Class in Selenium WebDriver
This Select class provides so many helper methods with the help of those methods we can perform multiple operations on the dropdown and multiple select elements. and you need to pass the element details on which you want to perform operations.
WebElement element = driver.findElement(By.id("testing")); Select oSelect = new Select(element);
Note: Select class only works for those elements who are inside <select> tags.
After creating the object of the select class now, you can get perform different operations with the help of the object. like below
Select Methods of Select Class
Now we are going to learn different methods by which we can able to select the values from a dropdown or multiple select. Here are some of the methods which we are going to use during selecting the values from a dropdown
Let’s try all the above methods one by one with an example:
selectByVisibleText(): This is a straightforward option to select a value from a drop-down and multiple select boxes. This method takes a string argument, which is one of the values of that selected element.
selectByIndex(): This method also works like selectByVisibleText, but the only difference is here we need to provide the index of the required element. Based on the index value, it returns the element.
selectByValue(): Its also similar to selectByVisibleText and selectByIndex, but the difference is it asks for the value which is described in the DOM that is inside the value attribute like value=”5″. Based on the value, it returns the element.
getOptions(): This method get the all option present inside the Select tag and returns List<WebElements>.
Select oSelect = new Select(driver.findElement(By.id("yy_date_8"))); List <WebElement> elementCount = oSelect.getOptions(); System.out.println(elementCount.size());
Note: You can use all the above methods in both DropDown and Multiple Select Boxes.
Different Deselect methods
The Way we have selected the values by using the different methods, in the same way, we can also deselect the selected values. The only difference is the above-selected methods are work for both dropdown and multiple select boxes, but the deselect methods only work for multiple boxes.
Here the available methods are deselectall(), deselectByIndex, deselectByValue and deselectByVisibletext.
- deselectAll(): It Helps you to deselect all select the values.
- deselectByIndex(): It will deselect the given index value only.
- deselectByValue(): It will deselect the value of matching argument.
- deselectByVisibleText(): Deselect all the value what display the matching text.
We can use the isMultiple() method to verify that the text box is supported to select multiple values or not. When we execute the method, it will return a boolean value, and based on that, we can find out. If it returns true, that means it selects multiple select, and the false value represents that the box does not support the multiple select.