Selenium-12 || Select Class to handle drop-down.

The Select class contains all the basic methods to handle the drop-down. Also, these are very easy to use and understandable.

Basically, there are two types of drop-down in HTML

  1. Drop-down having single option selectable
  2. Drop-down having multiple options selectable

Select Class methods

In the below example we have used basic methods to handle drop-down in which we can select only one value at a time.

Frequently used Methods

MethodFeature
isMultiple()To check if drop-down is single or multiple options selectable.
selectByIndex(index)To get the option via 0 based index.
selectByVisibleText(string)To get the option value based on the visible text. This is case sensitive.
selectByValue(string)To get the option based on the attribute value. This is case sensitive.
getOptions()To extract and create a List<WebElement> list for all the options in the drop-down.
getFirstSelectedOption()To get selected option as a WebElement for further use.

Code Example

public static void main(String[] args) throws InterruptedException, AWTException {

		System.setProperty("webdriver.chrome.driver", "c:\\chromedriver.exe");
		WebDriver driver = new ChromeDriver();

		String w3SURL="https://www.w3schools.com/tags/tryit.asp?filename=tryhtml_select";
		
		driver.get(w3SURL);
		
		driver.switchTo().frame("iframeResult");
		WebElement dropdownElement=driver.findElement(By.xpath("//select[@id='cars']"));
		
		Select select=new Select(dropdownElement);
		// Verfy if user can select multiple option in dropdown.
		System.out.println("Is multiple value selectable: "+select.isMultiple());
		// Select option via 0 based index
		select.selectByIndex(2);
		
		// Select option via visible text (Text is case sensitive)
		select.selectByVisibleText("Saab");
		
		//Select option via value attribute (Text is case sensitive)
		select.selectByValue("audi");
		
		// Extracting a list of elements for all the drop-down options
		List<WebElement> optionList= select.getOptions();
		for (WebElement webElement : optionList) {
			System.out.println(webElement.getText());
		}

// Get selected option as WebElement
		WebElement seletedElement=select.getFirstSelectedOption();
		System.out.println("Selected Element: "+seletedElement.getText());
}

Select Class with dropdown having multiple selectable options

In this drop-down, the user can select multiple options. This can be done manually by pressing the Control key and then click on options.

Frequently used Methods

All the methods we have discussed previously will be applicable here also. Below are the additional methods for the drop-down having multiple selectable options.

MethodFeature
getFirstSelectedOption()To get selected option as a WebElement for further use.
getAllSelectedOptions()Extract a list of all selected options as List<WebElement>.
deselectByIndex(index)To deselect the option by the index value.
deselectByValue(arg0)Deselect an option by the Value attribute.
deselectByVisibleText()Deselect an option by visible text.
deselectAll()Deselect all the selected options in one go.

Code Example

public static void main(String[] args) throws InterruptedException, AWTException {

		System.setProperty("webdriver.chrome.driver", "c:\\chromedriver.exe");
		WebDriver driver = new ChromeDriver();

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

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

		WebElement multDDElement = driver.findElement(By.xpath("//select[@id='cars']"));
		Select select1 = new Select(multDDElement);

		// Verify if user can select multiple option in dropdown.
		System.out.println("Is multiple value selectable: " + select1.isMultiple());

		// To select options via 0 based index
		select1.selectByIndex(1);
		
		// To select options via visible text
		select1.selectByVisibleText("Audi");
		
		// To select options via Value attribute
		select1.selectByValue("opel");

		// Extract first selcted element in the drop-down WebElement
		WebElement firstElement = select1.getFirstSelectedOption();
		System.out.println("First Selected Option: " + firstElement.getText());
		
		// Extract list of all selected option as WebElement.
		List<WebElement> selectedElements= select1.getAllSelectedOptions();
		System.out.println("\nAll selected options:");
		for (WebElement webElement : selectedElements) {
			System.out.println(webElement.getText());
		}
		
		// Deselect option via index
		select1.deselectByValue("opel");
		
		// Deselect all the options at once.
		select1.deselectAll();
}

Selenium Tutorials:


Leave a Comment

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

   YouTube ChannelQuora

            Ashok Kumar is working in an IT Company as a QA Consultant. He has started his career as a Test Trainee in manual testing in August 2010. Then he moves towards the automation testing after 4 years. He started learning JAVA and Selenium by self to get the knowledge of automation.

       While learning these tools and working on multiple projects, he found that sometimes people get stuck in live scenarios in their project and they have to do lots of RnD to get out of it. So he decided to start blogging only for such scenarios, where anyone facing any problem in their project, can ask any question or give a solution or you can say an alternate solution to achieve the goal successfully.

Later on, he observed that some people want to learn Java but they have few questions in their mind like how to start Java, whether we should go for the online or offline course. So he started writing tutorials on Java, Jira, Selenium, Excel etc.