I have a list of li elements wrapped in sub-menu class of HTML, before clicking on any of the li element I wanted to cross check all li elements are present with getText() and length but not sure how to go with length as I don't see any suggestion to go for length.

    WebElement menuList =  driver.findElement(By.id("menu-item-33"));
    List<WebElement>elems = driver.findElements(By.className("sub-menu"));
      for (WebElement ddlList : elems)
        //Something like ddlList.length(); but not able to work upon it.

  • 1
    ddlList is an individual element . . . if you just want to know how many you had, you want the length of elems . .
    – ernie
    Commented Mar 21, 2017 at 0:38

2 Answers 2


There are two ways/solutions to print num of element.

Solution 1 :

int i=0;
for (WebElement ddlList : elems)
   System.out.println("Element Num "+ i +" is "+ ddlList.getText());
  • Here just increase value of i in each repetition.

Solution 2 :

for (int i=0; i < elems.size(); i++)
   System.out.println("Element Num "+ i +" is " + elems.get(i).getText());
  • Here simple java loop with size() of list.

If you want to print only num of elements then follow below :


I believe you found the answer. But I would like to share this line of code just in case if this could also add to help. This is a C# code

MyTickets = driver.FindElements(By.CssSelector("#support-dashboard-form>div>div.dashboard-container>div")).Count();
    for (int i=2;i<=MyTickets+1;i++)
                string TicketRef = driver.FindElement(By.CssSelector("#support-dashboard-form>div>div.dashboard-container>div:nth-child(" + i + ")>div.column.dashboard-column-icon>div.ticket-ref")).Text;

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.