Tuesday, December 1, 2015

How to select item from Dropdown List / Combo box

While testing Application many times we need to select the values from combo box/dropdown. Here is the snippet shows how we can select values.
There are three ways to active it:-

1. selectByValue
2. selectByIndex
3. selectByVisibleText

Here is the snippet one can use to select item from dropdown box / combo box :-

     WebElement select =   driver.findElement(By.xpath("GiveXpathOfElement"));      
 Select s = new Select(select);


  •     if you want to select "ABC" from drop down then :
        String stringToSelect="ABC";
         s.selectByValue(stringToSelect);

  •    if you want to select value  from drop down  which is at index 1 then :
       s.selectByIndex(1);


  •    if you want to select visiable value from drop down  which is at index 1 then :
        String visiableValue="XYZ";
        s.selectByVisibleText(visiableValue);