ATA
ATA

Reputation: 51

How to show the selected options from a multi select drop down using selenium java?

I'm trying to show all the selected options from a multi select drop down list. But not getting the proper way to do this. Please help me on this.

Here is the html code for the drop down:

<select multiple id="fruits">
     <option value="banana">Banana</option>
     <option value="apple">Apple</option>
     <option value="orange">Orange</option>
     <option value="grape">Grape</option>
</select>

Here is the code i'm trying with:

public void dropDownOperations()
    {
        driver.get("http://output.jsbin.com/osebed/2");
        Select DDLIST = new Select(driver.findElement(By.id("fruits")));
        DDLIST.selectByIndex(0);
        String currentvalue = DDLIST.getFirstSelectedOption().getText();
        System.out.println(currentvalue);
        DDLIST.selectByIndex(1);
        String currentvalue1 = DDLIST.getFirstSelectedOption().getText();
        System.out.println(currentvalue1);          
    }

I also tried with this code:

Here i'm getting this output:

[[[[[ChromeDriver: chrome on XP (69aee19e9922ca218ff47c0ccdf1bbbc)] -> id: fruits]] -> tag name: option], [[[[ChromeDriver: chrome on XP (69aee19e9922ca218ff47c0ccdf1bbbc)] -> id: fruits]] -> tag name: option]]

public void dropDownOperations1()
    {
        driver.get("http://output.jsbin.com/osebed/2");
        Select DDLIST = new Select(driver.findElement(By.id("fruits")));
        DDLIST.selectByIndex(0);
        DDLIST.selectByIndex(1);
        List<WebElement> currentvalue1 = DDLIST.getAllSelectedOptions();
        System.out.println(currentvalue1);          
    }

Upvotes: 3

Views: 2270

Answers (5)

Jainish Kapadia
Jainish Kapadia

Reputation: 2611

Try this below code, It will select one by one options from dropdown.

Select DDLIST = new Select(driver.findElement(By.id("fruits")));
DDLIST.selectByIndex(0);
DDLIST.selectByIndex(1);

List<WebElement> selectedOptions = DDLIST.getAllSelectedOptions();
for(int i=0; i<selectedOptions.size(); i++)
{
    System.out.println(DDLIST.getOptions().get(i).getText());
}

Upvotes: 2

Kushal Bhalaik
Kushal Bhalaik

Reputation: 3384

Try This:

  List<WebElement> allSelected = select.getAllSelectedOptions();

      Iterator itr = allSelected.iterator();

      while(itr.hasNext()){

          WebElement item = (WebElement) itr.next();

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

Upvotes: 2

Sudharsan Selvaraj
Sudharsan Selvaraj

Reputation: 4832

Your second approach should work fine with a minor fix. getAllSelectedOptions() will return a List of selected options as WebElement. You need to iterate over the list to get the text from WebElement.

List<WebElement> selectedOptions = DDLIST.getAllSelectedOptions();
for (WebElement option : selectedOptions){
        System.out.println(option.getText());
}

Upvotes: 2

photobomber
photobomber

Reputation: 1

Try this:

Select DDLIST = new Select (driver.findElement(By.id("fruits")));               
for(int i=0; i<DDLIST.getOptions().size(); i++)
   System.out.println(DDLIST.getOptions().get(i).getText());

Upvotes: 0

FreakTester
FreakTester

Reputation: 57

Try This:

    System.out.println(DDLIST.selectByIndex(0).getText());
    System.out.println(DDLIST.selectByIndex(1).getText());

And so on. Instead of using a variable and trying it.

Upvotes: 1

Related Questions