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

39,810

Solution 1

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

Solution 2

String[] options = driver.findElement(By.id("dropdown")).getText().split("\n");
options.length;

Solution 3

Use .getXpathCount() method

int numOptions = selenium.getXpathCount("//*[@id='ddlTablePay']/option").intValue();
Share:
39,810
Jaydev
Author by

Jaydev

Updated on September 27, 2020

Comments

  • Jaydev
    Jaydev over 3 years

    I have Select Dropdown list with this:

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

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