Use Selenium Webdriver To Select Options From DropDown List

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 org.openqa.selenium.support.ui.Select class to implement this. In this article we will introduce how to use this class and all it’s methods step by step.

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

Constructor

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

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.

Choose Option Methods

  1. void selectByIndex(int index): Pick the option in the select list by the give index. Start from 0. Accomplished by checking the html option’s index.
  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 option is
    <option value="linux">Linux</option>

    then use selectByValue(“linux”) to choose it.

  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
    <option value="android">Android</option>

    then use selectByVisibleText(“Android”) to choose it.

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>

Check Multiple Method

  1. boolean isMultiple(): Check whether this dropdown list allow multiple selection or not.
    Achieve this by examining html SELECT tag’s “multiple” attribute’s value.

Example

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

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

Source Code

  1. TestDropDownInSelenium.html
    selenium example auto select in drop down list

    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);
    		}
    	}

To run below example, you need create below folder C:/Workspace and copy TestDropDownInSelenium.html into that folder.

Download “Select-DropDown-List-Options-Example-Code.zip” Select-DropDown-List-Options-Example-Code.zip – Downloaded 75 times – 2 KB

 

(Visited 2,816 times, 1 visits today)
READ :   POM(Page Object Model) Framework And Page Factory In WebDriver

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.