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
- Drop-down having single option selectable
- 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
Method | Feature |
---|---|
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.
Method | Feature |
---|---|
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:
- Selenium-12 || Select Class to handle drop-down.
- Selenium-11 || Use of Actions and Action Classes.
- Selenium-10 || Taking Screenshots using Selenium
- Selenium-9 || Understanding WebDriver API.
- Selenium-8 || Implementing Wait(s) in Selenium.
- Selenium-7 || Let’s learn to create complex XPath.
- Selenium-6 || XPath is the best way to locate web elements.
- Selenium-5 || Locating web elements using various type of Locators.
- Selenium-4 || Handling multiple web browsers.
- Selenium-3 || First program using Selenium Web Driver.
- Selenium-2 || Let’s learn Selenium IDE.
- Selenium-1 || Understanding Selenium and Selenium WebDriver.