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