Take the 2-minute tour ×
Software Quality Assurance & Testing Stack Exchange is a question and answer site for software quality control experts, automation engineers, and software testers. It's 100% free, no registration required.

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

share|improve this question

marked as duplicate by Yamikuronue, Bruce McLeod Feb 18 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.

    
What you tried so far? Did google gave you any suggestions? –  Peter Masiar Aug 1 '14 at 13:34
4  
5 seconds of googling (java+ selenium + title of your question) resulted in this: stackoverflow.com/questions/20210853/… –  Peter Masiar Aug 1 '14 at 21:54

4 Answers 4

up vote 0 down vote accepted

In watir, this can be accomplished in one line:

browser.select_list(:id=> 'id').options.count
share|improve this answer

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.

share|improve this answer
WebElement selectElement = driver.findElement(By.id("id"));
Select listBox = new Select(selectElement);
int size1 = listBox.getOptions().size();
share|improve this answer

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.

share|improve this answer

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