I have Select Dropdown list with this:
xpath //*[@id="ddlTablePay"]
I need to count the number of options in this drop-down. Thank You
I have Select Dropdown list with this:
xpath //*[@id="ddlTablePay"]
I need to count the number of options in this drop-down. Thank You
Use .getOptions()
method and store them in a list .Then find its size.
Select se = new Select(driver.findElement(By.id("select drop down locator")));
List<WebElement> l = se.getOptions();
l.size();
-Ajay
String[] options = driver.findElement(By.id("dropdown")).getText().split("\n");
options.length;
Use .getXpathCount()
method
int numOptions = selenium.getXpathCount("//*[@id='ddlTablePay']/option").intValue();
optionItems = Select(driver.find_element_by_xpath("//select[@id='ddlTablePay']"))
print "Total Elements " + str(len(optionItems.options))
//To count the number of options
Select dropDown = new Select(driver.findElement(By.id("ddlTablePay")));
List<WebElement> elementCount = dropDown.getOptions();
System.out.println("Number of items: " + elementCount.size());
//To get and print all the Options
Select dropDown = new Select(driver.findElement(By.id("ddlTablePay")));
List <WebElement> elementCount = dropDown.getOptions();
int itemSize = elementCount.size();
for(int i = 0; i < itemSize ; i++){
String optionsValue = elementCount.get(i).getText();
System.out.println(optionsValue);
}
Pass the xpath of the dropdown whose count you want:
static int getDropdownListCount(String xpath) {
WebElement element = driver.findElement(By.xpath(xpath));
Select select = new Select(element);
List<WebElement> elements = select.getOptions();
return elements.size();
}
It'll return count of total number of options under that dropdown.