Skip to main content


Identifying optgroup values in Dropdown – For Selenium automation

Select class in selenium WebDriver provides different methods to handle dropdown. We can perform operations such as finding number of options, getting the option values in a dropdown and selecting any options in a dropdown.

Have you ever come across a scenario to pick values from a particular group in dropdown box or to validate the options under each group in a dropdown?

I have a categorical dropdown as shown below and consider the options are dynamic.


On inspecting the element, HTML code looks as follows:


If the options in dropdown are fixed then it is very common to pick a value from particular group. We can simply use

new Select(driver.findElements(By.xpath(“//select[@id=’sel’]”))).selectByVisibleText(“Apples”);

but if the options in the dropdown group are dynamic then we cannot use the option value or index directly.

First we need to store all dropdown options under particular group in to  List and then we can select any Options using the List.

Normally we use below code to get the elements in dropdown

new Select(driver.findElement(By.xpath(“//select”))).getOptions();

If we use select class, it can only provide all elements in a dropdown

Even if you use xpath pointing to a particular group as below

new Select(driver.findElement(By.xpath(“//select/optgroup[@label=’Great Bands’]/option”))).getOptions();

It will throw an exception

“ Element should have been “select” but was “option””

We can directly use findElements instead of using select class to get elements in particular optgroup in a dropdown.

Below code will give all options under Great Bands group in a dropdown.

List<WebElement> GreatBands=driver.findElements(By.xpath(“//select/optgroup[@label=’Great Bands’]/option”));

Complete Code:

  public void testDropdown() throws Exception {
      List<WebElement> GreatBands=driver.findElements(By.
              xpath(“//select/optgroup[@label=’Great Bands’]”));
       for(WebElement opt:GreatBands){

And the output is:


We got the options under Great Bands group in a list. Similarly you can get options under any group in a dropdown by using the particular xpath.

Now to select first options under Great Bandages group:

new Select(driver.findElement(By.xpath(“//select”))).selectByVisibleText(GreatBands.get(0).getText());

Here GreatBands is a list in which we stored elements under Great Bands group.

Likewise you can perform any operation within a group in a dropdown by simply storing it in a list.

Validating and picking a value from a particular group in a dropdown becomes simple in selenium webDriver.

Thoughts on “Identifying optgroup values in Dropdown – For Selenium automation”

  1. Very nice explanation indeed. Thank you Ramya for the help. I could able to resolve this issue with your solutions.

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Follow Us