Reputation: 55
I have a dropdown with some countries as follows,
I want to send the values US,CA,AF,AL,DZ,AS,AD to an array, loop it and print by using Selenium and Java.
I tried the following
WebElement elementdrop = d.findElement(By.xpath("path"));
List<WebElement> dropdownvalues = d.findElements(By.xpath("path"));
for(WebElement value:dropdownvalues)
{
String pcvalues=value.getText();
System.out.println("value names" + pcvalues);
}
This will print United States Canada Afghanistan Albania etc. But I want like US CA AF AL DZ AS AD
Upvotes: 1
Views: 4806
Reputation: 136
WebElement dropdown = driver.findElement(By.name("Country"));
List<WebElement> options = dropdown.findElements(By.tagName("option"));
Iterator<WebElement> it=options.iterator();
while(it.hasNext())
{
System.out.println(it.next().getAttribute("Value"));
}
Give this a try and let me know if it works.
Upvotes: 3