Handling Dropdown menus using Selenium WebDriver with Java

Handling dropdown menu in Selenium a bit different than handling radio button and checkbox.

Again, dropdown menu is a group of elements like radio buttons and checkboxes, but we need to use certain Class and methods of Selenium WebDriver API in order to perform a set of operations on it.

Select class in Selenium WebDriver API : 

Select class is used to handle dropdown menu in Selenium WebDriver API.

The class has only one constructor.

public Select (WebElement element)

So, while creating an instance of Select class, we need to find the WebElement which holds locates the Select tag by any of the 8 locating strategy.

If the element references any element other than SELECT tag, then UnexpectedTagNameException is thrown.

So, basically, its used to convert a normal WebElement object into Select instance in order to perform a set of operations specific to Select tag i.e. dropdown menu.

WebElement categoriesDropDown = driver.findElement(By.name(“cat”));
Select categories = new Select(categoriesDropDown);

Different methods of Select class : 

Once you have converted the WebElement instance into Select class instance, you can perform any of the operation supported by Select class.

  1. Getting all options of dropdown menu
  2. Getting all selected options of dropdown menu
  3. Getting first selected option of dropdown menu
  4. Selecting an option from dropdown menu by INDEX
  5. Selecting an option by VISIBLE TEXT
  6. Selecting an option from dropdown menu by VALUE
  7. Deselecting an option from dropdown menu by INDEX
  8. Deselecting an option by VISIBLE TEXT
  9. Deselecting an option from dropdown menu by VALUE
  10. Deselecting all options
  11. Check whether dropdown supports multiple selection or not

 

getOptions()  –

The method returns the list of all the option tags associated with a Select tag (dropdown menu).

List<WebElement> optionsList = categories.getOptions();
for (WebElement option : optionsList) {
              System.out.println(option.getText());
}

getAllSelectedOptions() –

The method returns the list of all selected option tags associated with a Select tag (dropdown menu).

List<WebElement> allSelectedOptions = categories.getAllSelectedOptions();

getFirstSelectedOption() –

The method returns the first selected WebElement associated with a Select tag (dropdown menu).

WebElement firstSelected = categories.getFirstSelectedOption();

selectByIndex() – 

The method selects the option by Zero based INDEX value passed as an integer parameter.

categories.selectByIndex(0);

 selectByVisibleText() – 

The method selects the option by its visible option tag value. It accepts the visible text value of the option tag and returns nothing.

categories.selectByVisibleText(“orange”)

  selectByValue() – 

The method selects the option based on the VALUE attribute value of option tag. It accepts the VALUE attribute value as a string, selects the option tag with passed value, and returns nothing.

categories.selectByValue(“purple”)

deselectByIndex() –

The method deselects the option by Zero based INDEX value passed as an integer parameter.

categories.deselectByIndex(0);

deselectByVisibleText() – 

The method deselects the option by its visible option tag value. It accepts the visible text value of the option tag and returns nothing.

categories.deselectByVisibleText(“orange”);

 deselectByValue() –

The method deselects the option based on the VALUE attribute value of option tag. It accepts the VALUE attribute value as a string, selects the option tag with passed value, and returns nothing.

categories.deselectByValue(“purple”);

 deselectAll() –

The method deselects all the selected options associated with Select tag.

categories.deselectAll();

isMultiple() –

The method checks whether dropdown supports multiple selection or not :

System.out.println(categories.isMultiple());

 

Let us have a look at the below code working with dropdown menu in action.

import java.util.List;

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.support.ui.Select;

public class DropdownMenu {

            public static void main(String[] args) {
                        WebDriver driver = new FirefoxDriver();
                        driver.manage().window().maximize();
                        driver.get(“https://developer.mozilla.org/en/docs/Web/HTML/Element/select”);

                       // 1. getting drop down menu

                        WebElement categoriesDropDown = driver.findElement(By.name(“select”));
                       Select categories = new Select(categoriesDropDown);

                       // 2. getting all options of menu

                        List<WebElement> optionsList = categories.getOptions();
                        System.out.println(“The options list : “);
                        for (WebElement option : optionsList) {
                                 System.out.println(option.getText());
                        }

                       System.out.println();

                        // 3. selecting dropdown menu 

                       categories.selectByVisibleText(“Value 1”);

                       // categories.selectByIndex(0);
                      // categories.selectByValue(“”);

                      // 4. Deselecting dropdown menu
                      categories.deselectByVisibleText(“Value 1”);

                      // categories.deselectByIndex(0);
                      // categories.deselectByValue(“”);
                      // 5. Getting all selected options
                      List<WebElement> allSelectedOptions = categories.getAllSelectedOptions();

                      for (WebElement selectedOption : allSelectedOptions) {
                             System.out.println(“Selected option is : “+ selectedOption.getText());
                     }

                      // 6. Gets first selected option

                      WebElement firstSelected = categories.getFirstSelectedOption();
                      System.out.println(“First selected options is : “+ firstSelected.getText());

                      // 7. supports multiple select or not.

                      System.out.println(categories.isMultiple());

                      driver.quit();
           }
}

Hope it works for you!!

Cheers 🙂

Advertisements

#deselect-all-options, #deselect-by-index, #deselect-by-value, #deselect-by-visible-text, #get-all-dropdown-options, #handling-dropdown-menus-in-webdriver, #select-by-index, #select-by-value, #select-by-visible-text