I have Select Dropdown list with this:
xpath //*[@id="ddlTablePay"]
I need to count the number of options in this drop-down. Thank You
We can extract all the options in a dropdown in Selenium with the help of Select class which has the getOptions() method. This retrieves all the options on a Select tag and returns a list of web elements. This method does not accept any arguments.
The total number of links in a page can be counted with the help of findElements() method. The logic is to return a list of web elements with tagname anchor, then getting the size of that list.
How to select an option from drop-down menu? WebDriver provides three ways to select an option from the drop-down menu.
We can handle multi-select dropdown with Selenium webdriver using the Select class. A multi-select dropdown is the one which allows selection of multi options. getOptions – returns the list of all options in the dropdown.
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();
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With