Select Method in Selenium using Java

Select is one of the important class of selenium which provides several methods to handle the select drop down . objects and multiple select objects.

we can recognize the select objects in HTML by

Syntax for select 

Select sel=new Select(WebElement wb); // sel is object and WebElement is passed as the parameter


Package used for select Class

import org.openqa.selenium.support.ui.Select;


Methods of select class

1) selectByIndex(int) – Select the value of drop down by index – 1st element has index 0 , 2nd element has index 1 and so on . The index is passes as the parameter in the method.
2) selectByValue(String arg) – Select the value of drop down by value , the value is one of the value of the drop down. The value is passed as the string as the parameter in this method.
3) selectByVisibleText(String arg) – Select the value of drop down by text, the visible text is one of the value of the drop down. The visible text is passed as the string as the parameter in this method.

Note :  The value and visible text cannot be always same , some time value and text are different assigned to the elements of drop down. Below is the example where value is “volvocar” and text is “Volvo“.

<select id="singleselect" style="width: 250px;" name="selectdd">
<option value="volvocar">Volvo</option>
<option value="saabcar">Saab</option>
<option value="mercedes">Mercedes</option>
<option value="audi">Audi</option>
</select>

Select Drop Down – we can select only one value.

SELECT Tutotial in selenium

Java Program showing select methods

package com.test;

import java.util.concurrent.TimeUnit;

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 Select_Tutorial {

	public static void main(String[] args) throws InterruptedException {
		
		// setting the gecko driver path
		System.setProperty("webdriver.gecko.driver", "geckodriver.exe");
		 //openbrowser
		WebDriver driver=new FirefoxDriver();
		// implicit wait is defined with max waiting time of 10 seconds
		driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
		 //open url
		driver.get("https://padhle.com/automation-practice-demo/");
		// Fetching the webelement of select
		WebElement wb = driver.findElement(By.id("singleselect"));
		// passing the web element in select constructor 
		Select sb = new Select(wb);
		
		// select by index method - selecting the value by index
		// It will select the 2nd value - "Saab"
		sb.selectByIndex(1);		
		
		// stopping the execution for the next command for demo purpose only
		Thread.sleep(5000);
		
		// select by value method - selecting the value by value
		// It will select the volvo text		
		sb.selectByValue("volvocar");
		
		Thread.sleep(5000);
		
		// select by visible text method - selecting the value by text
		// It will select the Audi		
		sb.selectByVisibleText("Mercedes");
		
		Thread.sleep(5000);
		
		// we can also use send keys method directly on web element
		// it will select the Audi value
		wb.sendKeys("Audi");
		

		
}
}

Deselect Methods of Select class

1) deselectAll() – It will deselect all the selected elements of the drop down.
2) deselectByIndex(Int) – It will deselect the selected value of the drop down via index.The index is passes as the parameter in the method.
3) deselectByValue(String arg) – deselect the value of drop down by value , the value is one of the value of the drop down. The value is passed as the string as the parameter in this method.
4) deselectByVisibleText(String arg) – deselect the value of drop down by text, the visible text is one of the value of the drop down. The visible text is passed as the string as the parameter in this method.
5) isMultiple() – It is used to verify whether we can select multiple value from drop down or not . It returns true if we can select multiple values and return false if only one value can be selected at a time.
6) getOptions() – It is used to fetch all the elements of the drop down list. It returns the list of web element.
7) getAllSelectedOptions() – It will fetch all the selected option from the drop down.It returns the list of web element.
8) getFirstSelectedOption() – It will fetch the current selected option from the drop down.It returns the one web element.


Handling Multiple select drop down 

In Multiple select drop down we can select the multiple values simultaneously.

select-Tutorial

HTML code for multiple select statement

<select id="multipleselect" style="width: 250px;" multiple="multiple" name="multipledd">
<option value="volvo">Volvo</option>
<option value="saab">Saab</option>
<option value="opel">Opel</option>
<option value="audi">Audi</option>
</select>

Java Program shows deselect method and all

package com.test;

import java.util.List;
import java.util.concurrent.TimeUnit;

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 Select_Tutorial {

	public static void main(String[] args) throws InterruptedException {
		
		// setting the gecko driver path
		System.setProperty("webdriver.gecko.driver", "geckodriver.exe");
		 //openbrowser
		WebDriver driver=new FirefoxDriver();
		// implicit wait is defined with max waiting time of 10 seconds
		driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
		 //open url
		driver.get("https://padhle.com/automation-practice-demo/");
		// Fetching the webelement of select
		WebElement wb = driver.findElement(By.id("multipleselect"));
		// passing the web element in select constructor 
		Select sb = new Select(wb);
		
		// Is multiple method is used to check whether we can select the multiple 
		// values or not - will return true in this case
		if(sb.isMultiple())
		{
		// selecting multiple values - selecting all below values 
		sb.selectByIndex(1); // select Saab
		sb.selectByValue("volvo");// select Volvo
		sb.selectByVisibleText("Opel");// select Opel
		}
		
		// deselect methods ----
		// deselect by index method - deselecting the value by index
		//It will deselect the 2nd value - "Saab"
		sb.deselectByIndex(1);		
				
		// stopping the execution for the next command for demo purpose only
		Thread.sleep(5000);
				
		// deselect by value method - selecting the value by value
		// It will deselect the volvo text		
		sb.deselectByValue("volvo");
				
		Thread.sleep(5000);
		
		// deselect by visible text method - selecting the value by text
		//It will deselect the Opel		
		sb.selectByVisibleText("Opel");
				
		Thread.sleep(5000);
		
		// deselct all values in one go
		sb.deselectAll();
		
		// getOption fetch all elements of list 
		List<WebElement> wbl = sb.getOptions();		
		System.out.println(wbl.size()); // print the number of elements - 4
}
}

This is all about handling the select object in selenium. Hope you like this article.
To enroll in our selenium Online Training contact us or visit here.