Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to count the number of options in a select drop down box in Selenium WebDriver using Java?

I have Select Dropdown list with this:

xpath //*[@id="ddlTablePay"] 

I need to count the number of options in this drop-down. Thank You

like image 936
Jaydev Avatar asked Nov 26 '13 07:11

Jaydev


People also ask

How do you get all the values from drop down list in Selenium?

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.

How do you count the number of elements in a list in Selenium?

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 many ways are there to select a element from dropdown?

How to select an option from drop-down menu? WebDriver provides three ways to select an option from the drop-down menu.

How do I select multiple values from a dropdown in Selenium?

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.


3 Answers

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

like image 134
Ajay Avatar answered Oct 03 '22 08:10

Ajay


String[] options = driver.findElement(By.id("dropdown")).getText().split("\n");
options.length;
like image 27
IVI4K LI Avatar answered Oct 03 '22 09:10

IVI4K LI


Use .getXpathCount() method

int numOptions = selenium.getXpathCount("//*[@id='ddlTablePay']/option").intValue();
like image 42
Meena T Avatar answered Oct 03 '22 09:10

Meena T