how to select element in multi select box in selenium webdriver
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");
testing
Updated on July 09, 2022Comments
-
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>
-
Vishal over 10 yearsThe above can be used if you have a drop down/combo box.Index starts from 0.
-
Smeiff over 8 yearsThis is not answering the question. He asked about multi selection and your answer only works for a single selection.
-
Jack Miller over 5 yearsWhat/Where is
Utils
? Does it belong to Selenium or Java? -
Jack Miller over 5 yearsWhat is
ddObj
? How can this work: First you select something, then you sendCONTROL
, then you select the next item? -
NatNgs over 4 yearsLook once more,
Utils
is the example class name that contains theselectTheDropDownList
method described just here -
joshden over 3 yearsBe 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 over 2 yearsYour answer could be improved by adding extra information such as what your code does and how it helps the OP.