How can I get all elements from a drop down list? I used the code below:
List elements = driver.findElements(By.id(\"s\"));
There is a class designed for this in the bindigs.
You are looking for the Select
class:
https://code.google.com/p/selenium/source/browse/java/client/src/org/openqa/selenium/support/ui/Select.java
You would need to 'find' the actual select
element, not the individual options. Find that select
element, and let Selenium & the Select
class do the rest of the work for you.
You'd be looking for something like (s
being the actual select
element):
WebElement selectElement = driver.findElement(By.id("s");
Select select = new Select(selectElement);
The Select
class has a handy getOptions()
method. This will do exactly what you think it does.
List<WebElement> allOptions = select.getOptions();
Now you can do what you want with allOptions
.
List<WebElement> featureList;
featureList = Locate your Element;
for (WebElement i : featureList) {
System.out.println("\n********************** " + i.getText());
}
Use this hope it will helpful to you.
List WebElement allSuggestions = driver.findElements(By.xpath("Your Xpath"));
for (WebElement suggestion : allSuggestions)
{
System.out.println(suggestion.getText());
}
This will help to list all the elements from the dropdown:
Select dropdown = new Select(driver.findElement(By.id("id")));
//Get all options
List<WebElement> dd = dropdown.getOptions();
//Get the length
System.out.println(dd.size());
// Loop to print one by one
for (int j = 0; j < dd.size(); j++) {
System.out.println(dd.get(j).getText());
}
Select drop = new Select(driver.findElement(By.id("id")));
List<WebElement> dd = drop.getOptions();
System.out.println(dd.size());
for (int j = 0; j < dd.size(); j++) {
System.out.println(dd.get(j).getText());
}