Monday 21 January 2013

How to count the number of contents (Values) in the drop down using selenium webdriver?


How to count the number of contents (Values) in the drop down using selenium webdriver?




First we need to store the contents of DDL into one string. Then we need to split that string by line (\n).

Normally it will count "-Select-" as a string. So we need minus 1 from the array length.

 WebElement J = driver.findElement(By.id("ddlType"));
    String text = J.getText(); 
    String[] DDLcount =text.split("\n");
    System.out.println("Count of DDL contents:" +(DDLcount.length-1));


3 comments:

  1. Hi Ramaguru,

    I tried use your posted code. The out put is displaying 0 all the time.
    Can you please help, How to do?

    Thanks,
    Priya

    ReplyDelete
    Replies
    1. Hi Priya,

      1. Cross check DDL id
      2. Print (J.gettext) value

      Dropdown contents will be print in console

      Delete
  2. This comment has been removed by the author.

    ReplyDelete