7

I have Select Dropdown list with this:

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

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

iamsankalp89
  • 4,363
  • 2
  • 13
  • 35
Jaydev
  • 133
  • 2
  • 3
  • 10

6 Answers6

11

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

Mark Rowlands
  • 5,209
  • 2
  • 27
  • 41
Ajay
  • 385
  • 1
  • 4
  • 11
3
String[] options = driver.findElement(By.id("dropdown")).getText().split("\n");
options.length;
Aleksandr M
  • 23,988
  • 12
  • 67
  • 136
IVI4K LI
  • 31
  • 1
2

Use .getXpathCount() method

int numOptions = selenium.getXpathCount("//*[@id='ddlTablePay']/option").intValue();
Rafael Winterhalter
  • 40,511
  • 13
  • 102
  • 180
Meena T
  • 21
  • 3
0
optionItems = Select(driver.find_element_by_xpath("//select[@id='ddlTablePay']"))
print "Total Elements " + str(len(optionItems.options))
Sharanabasu Angadi
  • 4,074
  • 8
  • 41
  • 64
0

//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);
        }
Ripon Al Wasim
  • 35,466
  • 40
  • 150
  • 172
  • I have used ID instead of xpath though xpath also definitely works. ID is more preferable than xpath if any control/element has ID. – Ripon Al Wasim Sep 02 '16 at 12:11
-2

Select selection= new Select(driver.findElement(By.id("Drop down id")));

int size=selection.getOptions().size();

  • This questions already has the solutions you are proposing as answer. There is no need to write it again especially on a question that is almost 5 years old. – vl4d1m1r4 Sep 26 '18 at 20:57