How to get list of values from Drop down field

Getting all the options from Drop down field is easy task. We need to use method getOptions() which will give list of options in the form of Webelements. We can convert webelement list in String using getText() method and can save it in desired List. Below code snippet will help to get all the options from drop down:
Select select = new Select(driver.findElement(By.id(“dropdown_7”)));
// This will get all the options from drop down.
List<WebElement> list = select.getOptions();  
List<String> stringOptions = new ArrayList<String>();
// This will get text from the webelement and save it to some other List
for(WebElement s: list)
{ stringOptions.add(s.getText().trim());
}
// This will print all the String options.
for(String h : stringOptions)
{ System.out.println(h);
}

Comments

Popular posts from this blog

API Concepts