-1

This question already has an answer here:

working on selenium + java,. can any one help me... I have a drop down list box, and need to get the number of elements in the list box.

<select id="id">
<option value="1">1</option>
<option value="2">2</option>
<option value="3">3</option>
</select>

need to get the count, ie, 3..... thanks and in advance

marked as duplicate by Yamikuronue, Bruce McLeod Feb 18 '15 at 10:23

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

1

In watir, this can be accomplished in one line:

browser.select_list(:id=> 'id').options.count
4

Try this , storing all the Web elements of Select list in a List and then getting its size.

List<WebElement> optionCount = driver.findElements(By.xpath("//select/option"));
System.out.println(optionCount.size());

Thanks.

1
WebElement selectElement = driver.findElement(By.id("id"));
Select listBox = new Select(selectElement);
int size1 = listBox.getOptions().size();
0

try this,

Select s = new Select(driver.findElement(By.id("#id")));
List<WebElement> list = s.getOptions();

There you have all the elements of your dropdown, and using the size() method you'll have all the options.

Not the answer you're looking for? Browse other questions tagged or ask your own question.