This is article we will learn how to handle Dropdown in selenium and Multiple Select operations.
Select Option from Drop-Down Box
Following is a step-by-step process on how to select value from dropdown in Selenium.
Before handling dropdown in Selenium and controlling drop-down boxes, we must do following two things.
- Import packages org.openqa.selenium.support.ui.Select
- Instantiate the drop-down box as an object, Select in Selenium WebDriver
How to inspect web element for Drop-Down box.
Inspect element and check if there is Select tag associated with that web element which clearly indicates that we can use Select class of Selenium web driver and create object of that class and work with that to take actions on that drop down.
Let us check sample code for Select Drop down using selectByIndex() method.
package tests;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.support.ui.Select;
public class SelectDropDown {
public static void main(String[] args) {
System.setProperty("webdriver.chrome.driver", "C:\\chromedriver.exe");
WebDriver driver = new ChromeDriver();
driver.manage().window().maximize();
driver.get("https://rahulshettyacademy.com/AutomationPractice/");
//Create object of Select class and select option using selectByIndex()
Select s = new Select(driver.findElement(By.id("dropdown-class-example")));
s.selectByIndex(3);
}
}
Let us check sample code for Select Drop down using selectByValue() method.
package tests;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.support.ui.Select;
public class SelectDropDown {
public static void main(String[] args) {
System.setProperty("webdriver.chrome.driver", "C:\\chromedriver.exe");
WebDriver driver = new ChromeDriver();
driver.manage().window().maximize();
driver.get("https://rahulshettyacademy.com/AutomationPractice/");
//Create object of Select class and select option using selectByValue()
Select s = new Select(driver.findElement(By.id("dropdown-class-example")));
s.selectByValue("option2");
//close chrome driver
driver.close();
}
}
Let us check sample code for Select Drop down using selectByVisibleText() method.
package tests;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.support.ui.Select;
public class SelectDropDown {
public static void main(String[] args) {
System.setProperty("webdriver.chrome.driver", "C:\\chromedriver.exe");
WebDriver driver = new ChromeDriver();
driver.manage().window().maximize();
driver.get("https://rahulshettyacademy.com/AutomationPractice/");
//Create object of Select class and select option using selectByVisibleText()
Select s = new Select(driver.findElement(By.id("dropdown-class-example")));
driver.findElement(By.id("dropdown-class-example")).click();
s.selectByVisibleText("Option1");
//close chrome driver
driver.close();
}
}
Selecting Items in a Multiple SELECT elements.
We can use any methods in order to select multiple elements, selectByIndex(), selectByValue() & selectByVisibleText().
Let us check sample code for multiple select Drop down using selectByVisibleText() & selectByIndex() method.
package tests;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.support.ui.Select;
public class SelectDropDown {
public static void main(String[] args) {
System.setProperty("webdriver.chrome.driver", "C:\\chromedriver.exe");
WebDriver driver = new ChromeDriver();
driver.manage().window().maximize();
driver.get("http://output.jsbin.com/osebed/2");
//Create object of Select class and select option using selectByVisibleText()
Select s = new Select(driver.findElement(By.id("fruits")));
if (s.isMultiple()){
s.selectByVisibleText("Apple");
s.selectByIndex(3);
s.selectByValue("banana");
}
//close chrome driver
driver.close();
}
}
Summary
Element | Command | DESCRIPTION |
---|---|---|
Drop-Down Box | selectByVisibleText() | selects/deselects an option by its displayed text |
selectByValue() | selects/deselects an option by the value of its "value" attribute | |
selectByIndex() | selects/deselects an option by its index | |
isMultiple() | returns TRUE if the drop-down element allows multiple selection at a time; FALSE if otherwise | |
deselectAll() | deselects all previously selected options |