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();
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.
you can use the below syntax to count the number of element in drop down list. First you need to read anyone element from drop down list as below.
String report1 = driver.findElement(By.xpath("xpath of first element of dropdown list")).getText();
String [] element = driver.findElement(By.xpath("your-xpath-complete drop down section")).getText().split("\n");
elements.length
elements.length is the number of elements present in your drop down list
© 2022 - 2024 — McMap. All rights reserved.