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

7

8

I have Select Dropdown list with this:

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

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

Justiceship answered 26/11, 2013 at 7:23 Comment(4)
iterate it and simple increase counter by oneAnesthesia
dont we have any inbuilt function for that?Justiceship
Why haven't you accepted one of the answers?Jotun
As the element as ID, you should use ID instead of xpath. Because ID is more preferable than xpath.Lebrun
A
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();
Antispasmodic answered 26/11, 2013 at 7:44 Comment(0)
I
3
String[] options = driver.findElement(By.id("dropdown")).getText().split("\n");
options.length;
Insouciant answered 29/9, 2015 at 11:9 Comment(1)
maybe elaborate on the solution?Albina
L
2

Use .getXpathCount() method

int numOptions = selenium.getXpathCount("//*[@id='ddlTablePay']/option").intValue();
Lunate answered 16/1, 2014 at 12:38 Comment(0)
F
0
optionItems = Select(driver.find_element_by_xpath("//select[@id='ddlTablePay']"))
print "Total Elements " + str(len(optionItems.options))
Fukien answered 16/9, 2015 at 10:33 Comment(0)
L
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);
        }
Lebrun answered 2/9, 2016 at 12:10 Comment(1)
I have used ID instead of xpath though xpath also definitely works. ID is more preferable than xpath if any control/element has ID.Lebrun
M
0

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.

Maldives answered 8/10, 2022 at 5:38 Comment(0)
S
0

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

Salter answered 5/10, 2023 at 10:24 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.