'How can I select all the options in a drop down- Selenium Webdriver?

  • Currently working on Selenium WebDriver and using Java. If I have drop down name called Product..

  • In that drop down I have so many values (for ex:60). when executing the code I deselect all option then I selected which option I want because by default all values are selected in the HTML.. and it is working fine..

  • In the same way if I want to select all options at the same time.. How can I perform the action.

    <select id="productId" multiple="" style="width: 125px; display: none;" name="products[]">
    
    <option selected="" value="1020 ROUTER SERIES">1020 ROUTER SERIES</option>
    
    <option selected="" value="1030 ROUTER SERIES">1030 ROUTER SERIES</option>
    
    <option selected="" value="1040 ROUTER SERIES">1040 ROUTER SERIES</option>
    
    <option selected="" value="1061 ROUTER">1061 ROUTER</option>
    
     </select>
    

and so on..

Here is the sample code:

Log.info("Clicking on Product dropdown");
JavascriptExecutor executor31 = (JavascriptExecutor)driver;
executor31.executeScript("document.getElementById('ProductId').style.display='block';");
Select select31 = new Select(driver.findElement(By.id("ProductId")));
select31.deselectAll();
select31.selectByVisibleText("1222");
Thread.sleep(6000);
JavascriptExecutor executor32 = (JavascriptExecutor)driver;
 executor32.executeScript("document.getElementById('ProductId').style.display='block';");
Select select32 = new Select(driver.findElement(By.id("ProductId")));
select32.selectByVisibleText("1020");


Solution 1:[1]

You can not use anything similar to deselectAll(). However you can iterate through each option and select each time. Try following:

List<WebElement> liOp = new Select(driver.findElement(By.id("YourLocator"))).getOptions();
for(WebElement eachElem:liOp){
    new Select(driver.findElement(By.id("yourLocator"))).selectByVisibleText(eachElem.getText());
}

See whether it helps. For Control + A, try following:

Actions builder = new Actions(driver);
builder.sendKeys(Keys.chord(Keys.CONTROL,"a")).perform();

Solution 2:[2]

We get all the options to a list of webelements. Then we can iterate through this list to select all the options.

Select select31 = new Select(driver.findElement(By.id("ProductId")));
select31.deselectAll();

List<WebElement> select31Options = select31.getOptions();

for (WebElement option : select31Options) {
    select31.selectByVisibleText(option.getText());
}

Let me know if this helps you.

Solution 3:[3]

I suggest trying another solution, earlier I was using loop as well to select all elements in dropdown, but when their number is big it can take really long. What I tried and it worked was:

element(By.id("dropdownId")).selectByIndex(0);
element(By.id("dropdownId")).sendKeys(Keys.SHIFT, Keys.END);

I know it was a year ago but still it can help someone.

Solution 4:[4]

  1. First check if the drop down supports multiple selection.
  2. If multiple selections is possible, collect all the options of the select into a list.
  3. Use a for loop to iterate over all the elements in the list and select them.

    Select selectElement = new Select(driver.findElement(By.Id("productId")));
    if (selectElement.isMultiple()) {  /* step 1 */
        List<WebElement> options = selectElement.getOptions();  /* step 2 */
        for (WebElement we : options) {   /* step 3 */
            we.selectByVisibleText(we.getText());
        }
    } else {
        // does not support multiple
    }
    

Solution 5:[5]

driver.get("https://www.w3schools.com/tags/tryit.asp? 
filename=tryhtml_select_multiple");

driver.manage().window().maximize();

driver.switchTo().frame("iframeResult");

WebElement ele = driver.findElement(By.name("cars")); // Get control of select tag
Select select = new Select(ele);
List<WebElement> allOptions = select.getOptions();
ele.sendKeys(Keys.CONTROL); // to hold CTRL button once and then click on all options
for (WebElement webElement : allOptions) {
    webElement.click();
}
Thread.sleep(5000);
select.deselectAll(); // to deselect all values

Solution 6:[6]

    for (WebElement webElement : options) {
        webElement.click();
        System.out.println(webElement.getText());
    }

Inside the Enhanced for loop, added webElement.click(). This selects the web element. As this loop does not have any condition on it, all the values are selected.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1
Solution 2 Sighil
Solution 3 rusk4life
Solution 4 Revanth Kumar
Solution 5 Jeroen Heier
Solution 6 glenatron