0
 ***HTML***
    <select id="type" class="dropdownValues" name="type">
    <option class="dropdownValues" selected="selected" value="00">All</option>
    <option class="dropdownValues" value="01">Car</option>
    <option class="dropdownValues" value="02">House</option>
    <option class="dropdownValues" value="03">Boat</option>
    <option class="dropdownValues" value="04">Plane</option>
    <option class="dropdownValues" value="05">Tree</option>
    <option class="dropdownValues" value="06">Land</option>
    </select>

My Code

    Select selectBox = new Select(driver.findElement(By.id("type")));
    List<WebElement> selectOptions = selectBox.getOptions();
    for (WebElement temp : selectOptions) 
    {


     System.out.println(temp.getText());
        }

***The Output is displaying 7 times.
All
Car House Boat Plane Tree Land All Car House Boat Plane Tree Land All Car House Boat Plane Tree Land All Car House Boat Plane Tree Land All Car House Boat Plane Tree Land All Car House Boat Plane Tree Land

I would like to itterate through each options 1 times  and select them.
user2410266
  • 531
  • 2
  • 8
  • 22
  • possible duplicate of [Selenium WebDriver and DropDown Boxes](http://stackoverflow.com/questions/7232544/selenium-webdriver-and-dropdown-boxes) – Chris Baker Nov 07 '14 at 18:55

2 Answers2

1
public static void main(String[] args)
{
    WebDriver driver=new FirefoxDriver();
    driver.get("file:///D:/Programming%20Samples/SelectOptions.html");

    WebElement item=new WebDriverWait(driver,60)
                    .until(ExpectedConditions.elementToBeClickable(By.id("type")));
    Select listItem=new Select(item);
    for(Integer i=0;i<listItem.getOptions().size();i++)
    {
        listItem.selectByIndex(i);
        System.out.println(listItem.getFirstSelectedOption().getText()); //Just to make sure what is selected
    }
    driver.close();
}
Uday
  • 1,433
  • 10
  • 36
  • 57
0

I'm not super familiar with the WebDriver in Java, but in C# you would do something like this:

foreach (var elem in selectOptions){
   elem.Click(); //or elem.SendKeys(Keys.Enter);
}

See a duplicate question that has already been answered.

Community
  • 1
  • 1
sparkyShorts
  • 630
  • 9
  • 28