how to select element in multi select box in selenium webdriver

46,612

Solution 1

In a function pass a list of values use any delimiter lets say comma as a delimiter:

public static void selectMultipelValues(String multipleVals) {
   String multipleSel[] = multipleVals.split(",");

   for (String valueToBeSelected : multipleSel) {
      new Select(driver.findElement(By.id(propId))).selectByVisibleText(valueToBeSelected);
      driver.findElement(By.id(ddObj)).sendKeys(Keys.CONTROL);
   }
}

Solution 2

If you have Utils static method like this:

public static void selectTheDropDownList(WebElement dropDown,String text)
{
    Select select = new Select(dropDown);
    select.selectByVisibleText(text);       
}

and you can do like this, to select multiple options:

Utils.selectTheDropDownList(dropDown,text1);
Utils.selectTheDropDownList(dropDown,text2);
. . . 
Utils.selectTheDropDownList(dropDown,textn);

This should work.

Solution 3

This works for me:

final String[] textOptions = {"value1", "value2"};
final WebElement element = driver.findElement(By.id("someId"));
final Select dropdown = new Select(element);
final List<WebElement> options = dropdown.getOptions();
final Actions builder = new Actions(driver);
final boolean isMultiple = dropdown.isMultiple();
if (isMultiple) {
    dropdown.deselectAll();
}
builder.keyDown(Keys.CONTROL);
for (String textOption : textOptions) {
    for (WebElement option : options) {
        final String optionText = option.getText().trim();
        if (optionText.equalsIgnoreCase(textOption)) {
            if (isMultiple) {
                if (!option.isSelected()) {
                    builder.click(option);
                }
            } else {
                option.click();
            }
            break;
        }
    }
}
builder.keyUp(Keys.CONTROL).build().perform();

Solution 4

I have spent quite some time trying to simulate a click with the control key pressed with the webdriver for Chrome. After some investigation it appeared, that when you generate a click on an OPTION element of a multiselect, no click actually happens. Instead, a change event is genetared in the browser. This leads to the situation, where subsequent "clicks" on other options of the multiselect do not clear the previously selected options, which is sometimes unwanted behavior. To solve this I have come up with the following solution:

Actions actions = new Actions(driver);
if(controlNeeded)
    actions.keyDown(Keys.CONTROL);
actions.moveToElement((WebElement) option_you_want_to_click);
actions.clickAndHold();
actions.pause(100);
actions.release();
if(controlNeeded)
    actions.keyUp(Keys.CONTROL);
actions.build().perform();

This way you can select both single and multiple elements depending on the Ctrl key.

Solution 5

Use the below code, its simple and easy!! It worked for me.

Select dd1 = new Select(driver.findElement(By.name("swPacks[]")));
dd1.selectByVisibleText("ADVIP");
dd1.selectByVisibleText("ADVLEG");
Share:
46,612
testing
Author by

testing

Updated on July 09, 2022

Comments

  • testing
    testing almost 2 years

    Currently working on Selenium WebDriver and using Java.. I want to know to select values in Multi-select box. The options are already selected.. If i want to select any two or more option. how can perform the action.

    The HTML is follows:

    <select id="swpacksId" multiple="" style="width: 125px; display: none;" name="swPacks[]">
    <option selected="" value="ADVIP">ADVIP</option>
    <option selected="" value="ADVLEG">ADVLEG</option>
    <option selected="" value="ADVSEC">ADVSEC</option>
    <option selected="" value="Boot">Boot</option>
    <option selected="" value="H323">H323</option>
    <option selected="" value="IBC">IBC</option>
    <option selected="" value="MULTI">MULTI</option>
    <option selected="" value="None">None</option>
    </select>
    

    enter image description here

  • Vishal
    Vishal over 10 years
    The above can be used if you have a drop down/combo box.Index starts from 0.
  • Smeiff
    Smeiff over 8 years
    This is not answering the question. He asked about multi selection and your answer only works for a single selection.
  • Jack Miller
    Jack Miller over 5 years
    What/Where is Utils? Does it belong to Selenium or Java?
  • Jack Miller
    Jack Miller over 5 years
    What is ddObj? How can this work: First you select something, then you send CONTROL, then you select the next item?
  • NatNgs
    NatNgs over 4 years
    Look once more, Utils is the example class name that contains the selectTheDropDownList method described just here
  • joshden
    joshden over 3 years
    Be careful regarding taking a string and then splitting on comma (,). What if the visible text itself of one or more options have commas in them? Why not instead have selectMultipleValues take a string array directly?
  • Tyler2P
    Tyler2P over 2 years
    Your answer could be improved by adding extra information such as what your code does and how it helps the OP.