
Select Class in Selenium
Select Class in Selenium
There are different types of WebObjects that can be embedded in our webpage. Each has its own purpose, Likewise DropDown is one among them. It restricts the user to select the required option and also provide the user the convenience to select the option without the need to type it manually. Let’s start to create a DropDown value on our webpage.
<!DOCTYPE html> <html> <body> <div>Sample DropDown</div> <select> <option value="Besant">Besant</option> <option value="Smp">samp</option> <option value="Driver">Driver</option> <option value="Course">course</option> </select> </body> </html> |
WebElement element=null; Select besantSele =new Select(element); |

import org.openqa.selenium.support.ui.Select; |
selectByVisibleText(“besant”)
This is very much self-explanatory, as the name suggests we can make our object to select the dropdown value based on our Visible text on our browser.
selectByIndex(0)
This would select the value of the dropdown based on the index value starting from zero
selectByValue(“Driver”)
In our previous source code, we have added Value attribute along with string. We can also select the required value based on it.
Above all the methods would not return value.
There can be a scenario where we need to check the values inside the dropdown, In such cases, we don’t have any built-in method but we can extract all the options available in the DropDown and then we can iterate to check it.
Select obj= new Select(driver.findElement(By.id("678gjhb"))); List<WebElement> elementlist = obj.getOptions(); System.out.println(elementlist.size()); |
Select obj= new Select(driver.findElement(By.id("sdf343"))); if(obj.isMultiple()) { obj.selectByVisibleText(text) obj.selectByVisibleText(text) } else { obj.selectByVisibleText(text) } |
- deselect()
- deselectByIndex(int)
- deselectByValue(string)
- deselectByVisibletext(string)
These are not much-complicated methods. These are more self-explanatory and we can use the same way as we used the previous methods.