0

Saw few questions about this but I think that they are not giving the answer that I'm seeking since all of them were using Select class and I can't use that class since the drop-down is not using select tag.

So I can click and I can display all the options that are available in my drop-down list but I can't display the count for all of them.

I have this code for displaying all the options of my drop-down

List<WebElement> allText = driver.findElements(By.xpath("/html/body/div/div/div[3]/div/div/div[2]/div[2]/div[1]/div[3]/form/div/ul/li[1]/span/ul"));
   for ( WebElement element: allText) { 
       System.out.println(element.getText());
   }

but I can't display count for all the options in the list. I used this

System.out.println(allText.size());

on different places in the codebut it always showed 1. What should I use in order to show the count for all the elements in the drop-down ?

The HTML for the list looks something like this

<ul class="dropdown-menu" role="menu">        
                        <li><a pid="null" ng-click="option = null" href="">Option 0</a></li>                 
                        <li><a pid="1" ng-click="option = 1" href="">Option 1</a></li>               
                        <li><a pid="2" ng-click="option = 2" href="">Option 2</a></li>
                        <li><a pid="3" ng-click="option = 3" href="">Option 3</a></li>
                        <li><a pid="4" ng-click="option = 4" href="">Option 4</a></li>                  
                     </ul>

So I need to get the number 5 for the count.

8
  • by.xpath("//ul[@class='dropdown-menu']/li/a") strore in list<webelements> then use for loop it works..... Commented May 9, 2016 at 7:14
  • Tried this but I still can't get the count for elements in the ddl. Tried like this ` List<WebElement> allText1 = driver.findElements(By.xpath("//ul[@class='dropdown-menu']/li/a")); for ( WebElement element: allText1) { System.out.println(element.getText()); System.out.println(allText1.size()); }` also tried with putting the size expretion out from the for loop and still getting the same Commented May 9, 2016 at 12:51
  • you made mistake here ->"System.out.println(allText1.size()); " replce this with element.size(); Commented May 9, 2016 at 13:09
  • There is no method size() for type WebElement so this System.out.println(element.size()); won't work Commented May 9, 2016 at 13:25
  • Yeah tried that in the beggining but getsize() is giving the size in pixels. Commented May 9, 2016 at 13:31

1 Answer 1

0

There is 1 <UL> element, multiple <LI>. Use xpath to the <LI> not the <UL>.

You should end up with this.

List<WebElement> allText = driver.findElements(By.xpath("/html/body/div/div/div[3]/div/div/div[2]/div[2]/div[1]/div[3]/form/div/ul/li[1]/span/ul/li"));

3
  • Tried that but still getting count of 1 instead of 5. Commented May 9, 2016 at 12:53
  • Edited to reflect xpath required based on the xpath listed
    – Paul Muir
    Commented May 9, 2016 at 13:24
  • Yeah this worked. It gives the count of element for the selected ddl. Note:Only thing that I couldn't do is find the actual xPath using firebug, I could find the same xPath but without the last "/li". I guess that's why this xPath expression can't be used for both giving the count of elements and displaying its text content. Commented May 9, 2016 at 14:33

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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