How can I get all elements from a drop down list? I used the code below:
List elements = driver.findElements(By.id(\"s\"));
I took Facebook's registration page as an example it may help to understand.
Here is a code to get all month's names option as a list from the month's dropdown list.
List option = driver.findElements((By.xpath("//[@id='month']/option")));
ArrayList a = new ArrayList();
for (WebElement str: option)
{
String s = str.getText();
if(!s.equals("Month")) {
a.add(s);
}
else {
continue;
}
}
System.out.println("The Output is: "+ a);
Explanation of the above code is,
- Storing all the elements in a list.
- Declaring an empty Array list for storing options.
- By using for each loop to extract all the options one by one and store into the ArrayList.
- Printing all options as a list.
Hope this will help you.! Best Luck.!!