Webdriver Select with Multiple Attribute

WebDriver’s support classes called “Select”, which provides useful methods for interacting with select options. User can perform operations on a select dropdown and also de-select operation using the below methods.

User can also get the text of the values in the dropdown. Also can get the option which are selected by the user. To use these options, the Select Tag should have "multiple" attribute.

Method Name: getOptions

Syntax: select.getOptions ();
Returns: List
Purpose: Returns all the option elements displayed in this select tag (dropdown list)

Example:HTML Sample Code

<html>
<head>
<title>Multi select Drop Down List Box</title>
</head>
<body>
<p>What all devices do you listen to music on?</p>
<select name="Mobdevices" multiple="multiple"><option value="0" selected> Please select</option>
<option value="1">iPhone</option>
<option value="2">Nokia</option>
<option value="3">Samsung</option>
<option value="4">HTC</option>
<option value="5">BlackBerry</option>
</select>
</body>
</html>

Example Webdriver Code:

//To get all the options present in the dropdown
List<WebElement> allOptions = se.getOptions();
for (WebElement webElement : allOptions)
{
System.out.println(webElement.getText());
}

Method Name: getAllSelectedOptions

Syntax: select.getAllSelectedOptions();
Returns: List
Purpose: It will return all the option elements that are selected in the select tag.

Example Webdriver Code

WebElement element=driver.findElement(By.name("Mobdevices"));
Select se=new Select(element);
se.selectByVisibleText("Nokia");
se.selectByVisibleText("HTC");
//To get all the options that are selected in the dropdown.
List<WebElement> allSelectedOptions = se.getAllSelectedOptions();
for (WebElement webElement : allSelectedOptions)
{
System.out.println("You have selected ::"+ webElement.getText());
}

Method Name: getFirstSelectedOption

Syntax: Select.getFirstSelectedOption();
Returns: WebElement 
Purpose:  It will return the first selected option in this select tag (or the currently selected option in a normal select)

Example Webdriver Code:

WebElement element=driver.findElement(By.name("Mobdevices"));
Select se=new Select(element);
se.selectByVisibleText("Nokia");
se.selectByVisibleText("HTC");
//To get the first selected option in the dropdown
WebElement firstOption = se.getFirstSelectedOption();
System.out.println("The First selected option is::" +firstOption.getText());

Method Name: isMultiple

Syntax: select.isMultiple ();
Returns: Boolean
Purpose: To check whether the Select element supports selecting multiple options. This will be done by checking the value of the "multiple" attributes in Select tag.

Example:

if(se.isMultiple())
{
System.out.println("Select tag allows multiple selection");	
}
else
{
System.out.println("Select does not allow multiple selections");
}

The below example Webdriver code show all the above examples together

package com.first.example;
import java.util.Iterator;
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;
import org.testng.annotations.Test;

public class selectExamples {
	WebDriver driver;
	@Test
	public void selectSamples() throws InterruptedException
	{
		driver = new FirefoxDriver();
		driver.get("C:\\Users\\DEV\\Desktop\\html-and-css-code-samples\\Final Code\\chapter-07\\dropdown-select.html");
		WebElement element=driver.findElement(By.name("Mobdevices"));
		Select se=new Select(element);
		
		se.selectByVisibleText("Nokia");
		se.selectByVisibleText("HTC");
		
		//To get all the options present in the dropdown
		List<WebElement> allOptions = se.getOptions();
		for (WebElement webElement : allOptions)
		{
			System.out.println(webElement.getText());
		}
		
		//To get all the options that are selected in the dropdown.
		List<WebElement> allSelectedOptions = se.getAllSelectedOptions();
		for (WebElement webElement : allSelectedOptions)
		{
			System.out.println("You have selected::"+ webElement.getText());
		}
		 
		//To get the first selected option in the dropdown
		WebElement firstOption = se.getFirstSelectedOption();
		System.out.println("The First selected option is::" +firstOption.getText());
		
		if(se.isMultiple())
		{
		System.out.println("Select tag allows multiple selection");	
		}
	}
}

Please click the below link for Select options 'Part-1"
Select Options Part -I

Selenium Tutorials: 

Comments

Hello admin,
Glad to see your pages that helps us alot with useful stuffs. Please continue updating. And I'd request you to add WebServices based topics that are asked so much in IT nowadays.

so here is the link on which am working on http://demos.telerik.com/aspnet-ajax/listbox/examples/overview/defaultcs... . am using below code to select one of the item. can anyone help me to select the item. Thanks in advance here is the script am using.

WebElement listSource = driver.findElement(By.xpath(".//*[@class='rlbList']/li"));
//List<WebElement> listSource = driver.findElements(By.xpath(".//*[@class='rlbList']/li")); i tried with this line toooo
Select se = new Select(listSource);
//se.selectByVisibleText("Brazil");

Use the below code for selection for values within UL and LI tag
WebDriver driver = new FirefoxDriver();
driver.get("http://demos.telerik.com/aspnet-ajax/listbox/examples/overview/defaultcs...);
driver.manage().window().maximize();
Thread.sleep(5000l);
List<WebElement> drpCountry = driver.findElements(By.xpath("//ul[@class='rlbList']/li"));

for(WebElement el : drpCountry)
{
if(el.getText().equals("Brazil"))
{
el.click();
}

}

Add new comment