Check whether a dropdown is
Single value dropdown or Multi Value dropdown ?
We can verify whether a dropdown is single value
dropdown or multi value dropdown in many ways.
1. isMultiple - this method is a non-static method of Select class in selenium webdriver.
isMultiple method returns True if the dropdown is multi value dropdown otherwise it returns false for single value dropdown.
1. isMultiple - this method is a non-static method of Select class in selenium webdriver.
isMultiple method returns True if the dropdown is multi value dropdown otherwise it returns false for single value dropdown.
//open webpage
driver.get("https://the-internet.herokuapp.com/dropdown");
//find the dropdown
using xpath
WebElement dropdownElement =
driver.findElement(By.xpath("//select[@id='dropdown']"));
//create object for
Select class
Select dropdown = new
Select(dropdownElement);
//check single or
multi dropdown
boolean singleOrMultiple = dropdown.isMultiple();
if
(singleOrMultiple) {
System.out.println("Dropdown
is Multi value dropdown");
}else{
System.out.println("Dropdown
is Single value dropdown");
}
2. getAttribute("multiple") method
return true if the dropdown is multiple and returns false incase of single
value dropdown.
//open webpage
driver.get("application-url");
//find the dropdown
using xpath
WebElement dropdownElement =
driver.findElement(By.xpath("//select[@id='first']"));
//create object for
Select class
String singleOrMulti = dropdownElement.getAttribute("multiple");
//compare the
result
if
(singleOrMulti != null) {
System.out.println("Dropdown
is Multi value dropdown");
}else{
System.out.println("Dropdown
is Single value dropdown");
}
No comments:
Post a Comment