Use Selenium Webdriver To Select Options From DropDown List

The select dropdown list is a widely used Html web element in web pages. But how to choose it’s options automatically in selenium automation test scripts? Selenium webdriver provide a class org.openqa.selenium.support.ui.Select to implement this. In this article, we will introduce how to use this class and all it’s methods step by step.

1. Class org.openqa.selenium.support.ui.Select.

Class org.openqa.selenium.support.ui.Select is used to handle an Html dropdown list web element. Before using it you should import it in your java code. It has the below methods:

1.1 Constructor.

  1. public Select(WebElement element): This constructor uses an input web element to build a Select object.
  2. The input element must represent an Html SELECT tag, otherwise, an UnexpectedTagNameException will be thrown.

1.2 Get Options Methods.

  1. java.util.List<WebElement> getOptions(): Return all the dropdown list options web element in a list. You should iterate in the return list to get each option.
  2. java.util.List<WebElement> getAllSelectedOptions(): Return all the user picked options in a list.
  3. WebElement getFirstSelectedOption(): Return the first chosen option web element object in the dropdown list.

1.3 Choose Option Methods.

  1. void selectByIndex(int index): Pick the option in the select list by the given index. The index starts from 0, ends with the Html option’s index – 1.
  2. void selectByValue(java.lang.String value): Choose all the options that it’s value attribute match the given value. For example, if the Html select option is as below then we use selectByValue(“linux”) to choose it.
    <option value="linux">Linux</option>
  3. void selectByVisibleText(java.lang.String text): Choose the option in the list which option label matching the text argument. For example, if the Html option is as below then use selectByVisibleText(“Android”) to choose it.
    <option value="android">Android</option>

1.4 Deselect Option Methods.

  1. void deselectAll(): Deselect all picked options. Only valid if the html SELECT tag’s  multiple attribute’s value is true.
  2. void deselectByValue(java.lang.String value): Deselect all options which value attribute match the argument. For below html option
    <option value="windows">Windows</option>

    deselectByValue(“windows”) can be used to deselect it.

  3. void deselectByIndex(int index): Deselect the option at the given index.
  4. void deselectByVisibleText(java.lang.String text): Deselect all the options that option label match the input parameter text. For below option, we can use deselectByVisibleText(“MacOs”) to deselect it.
    <option value="macos">MacOs</option>

1.5 Check Multiple Method.

  1. boolean isMultiple(): Check whether this dropdown list allows multiple selections or not. Achieve this by examining the Html SELECT tag’s “multiple” attribute’s value.

2. Example.

This selenium automation test example will use Firefox to open a local web page. There is a dropdown list on the web page. It will do the following actions.

  1. Find the dropdown list web element by it’s id.
  2. Print all the options’ labels and values in the dropdown list.
  3. Test all select and deselect methods of Select class.
  4. Test getFirstSelectedOption() and getAllSelectedOptions() methods.

2.1 Source Code.

  1. TestDropDownInSelenium.html
    Please choose one or more os that you prefer:<br/><br/>
    <select id="os" multiple="true" width=3000px>
    	<option value="windows">Windows</option>
    	<option value="linux">Linux</option>
    	<option value="unix">Unix</option>
    	<option value="macos">MacOs</option>
    	<option value="ios">IOS</option>
    	<option value="android">Android</option>
    </select>
  2. com.dev2qa.webdriver.dropdown.TestDropDown
    @Test
    /* This method is used to test html SELECT web element operations in selenium webdriver.*/
    public void test() throws InterruptedException {
    
    	String pageUrl = "file://C:/WorkSpace/dev2qa.com/Code/src/com/dev2qa/webdriver/dropdown/TestDropDownInSelenium.html";
    	this.ffDriver.get(pageUrl);
    		
    	/* Find the drop down list web element by it's id. */
    	By dropDownById = By.id("os");
    	WebElement dropDownEle = this.ffDriver.findElement(dropDownById);
    		
    	if(dropDownEle!=null)
    	{
    		Select dropDownSelect = new Select(dropDownEle);
    			
    		/* Loop to print all the drop down options label and value.*/
    		System.out.println("Options for this drop down list.");
    		this.printOptionsForThisDropDownList(dropDownSelect.getOptions());
    			
    		/* If can choose multiple options. */
    		if(dropDownSelect.isMultiple())
    		{
    			/*Test selectByIndex(),selectByValue() and selectByVisibleText() methods. */
    			dropDownSelect.selectByIndex(1);
    				
    			Thread.sleep(1000);
    				
    			dropDownSelect.selectByValue("android");
    				
    			Thread.sleep(1000);
    				
    			dropDownSelect.selectByVisibleText("MacOs");
    		}else
    		{
    			dropDownSelect.selectByValue("linux");
    		}
    			
    		Thread.sleep(3000);
    			
    		/* Print out first selected option. */
    		WebElement firstSelectedOption = dropDownSelect.getFirstSelectedOption();
    		this.printDropDownOption(firstSelectedOption, "First selected option");
    			
    		Thread.sleep(3000);
    			
    		/* Loop to print all selected drop down options label and value.*/
    		System.out.println("Selected Options for this drop down list.");
    		this.printOptionsForThisDropDownList(dropDownSelect.getAllSelectedOptions());
    			
    		Thread.sleep(3000);
    		
    		/* If can select multiple options. */
    		if(dropDownSelect.isMultiple())
    		{
    			/*Test Select class's deselectByIndex(), deselectByValue() and deselectByVisibleText() methods. */
    			dropDownSelect.deselectByIndex(1);
    			System.out.println("Option at index 1 has been deselected.");
    			Thread.sleep(1000);
    				
    			dropDownSelect.deselectByValue("android");
    			System.out.println("Option has value android has been deselected.");
    			Thread.sleep(1000);
    			
    			dropDownSelect.deselectByVisibleText("MacOs");
    			System.out.println("Option at label MacOs has been deselected.");
    		}else
    		{
    			dropDownSelect.deselectByValue("linux");
    		}
    			
    		Thread.sleep(3000);
    			
    		this.selectAll(dropDownSelect);
    		System.out.println("Select all options.");
    		
    		Thread.sleep(3000);
    		dropDownSelect.deselectAll();
    		System.out.println("Deselect all options.");
    		
    	}
    }
    	
    /* Select all options in the drop down list. */
    private void selectAll(Select dropDownSelect)
    {
    	if(dropDownSelect!=null)
    	{
    		List<WebElement> optionsList = dropDownSelect.getOptions();
    		int size = optionsList.size();
    		for(int i=0;i<size;i++)
    		{
    			WebElement optionElement = optionsList.get(i);
    			String optionValue = optionElement.getAttribute("value");
    			dropDownSelect.selectByValue(optionValue);
    		}
    	}
    }
    	
    /* Loop in the html select tag options list and print each option's label and value.*/
    private void printOptionsForThisDropDownList(List<WebElement> optionsList)
    {
    	if(optionsList != null)
    	{
    		int size = optionsList.size();
    		for(int i=0;i<size;i++)
    		{
    			WebElement optionEle = optionsList.get(i);
    				
    			this.printDropDownOption(optionEle, "Option " + i);
    		}
    	}
    }
    	
    /* Print drop down option's label text and value attribute.*/
    private void printDropDownOption(WebElement optionEle, String message)
    {
    	if(optionEle!=null)
    	{
    		String value = optionEle.getAttribute("value");
    		String label = optionEle.getText();
    		
    		System.out.println(message + " , label = " + label + " , value = " + value);
    	}
    }
  3. To run the above example, you need to create folder C:/Workspace and copy TestDropDownInSelenium.html into that folder.

Leave a Comment

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.