top of page

Selenium Java Tutorial

Handling Drop-Downs

What Is The Concept Of Drop-Downs In Selenium ?

If we talk about a website, it has web pages that contains a range of various web elements including buttons, checkbox, drop- downs, text field, hyperlinks and so on... In this tutorial we are going to explain how to handle drop-down in selenium ?
'Select' Class
Selenium provides 'Select' class for drop downs that are created using HTML SELECT tag. First, we have to import the Select package.


Instance of 'Select' Constructor
Declare the drop-down element as an instance of the 'Select' class by creating an object for 'Select' class. Remember, 'Select' is a parameterized constructor and accepts only 'WebElement' as a parameter.

Select OldStyleSelectMenu=new Select(driver.findElement( ("oldSelectMenu")));

Methods for Controlling the Options of Drop-Down
For controlling the drop-down 'Select' class provides a number of methods as follows :- (a.) selectByVisibleText()
(b.) deselectByVisibleText()
Selects/Deselects the particular option by it's corresponding displayed text on the webpage.
parameter : String(Text)
(a.) selectByIndex()
(b.) deselectByIndex()
Selects/Deselects the particular option by it's corresponding index that starts from '0' .
parameter : int

(a.) selectByValue()
(b.) deselectByValue()
Selects/Deselects the particular option by it's corresponding value in the DOM.
parameter : String(Text)
To check if the dropdown allows multiple selections simultaneously if Yes then returns TRUE otherwise FALSE.
Parameter : No Parameters
Returns : Boolean(True/False)

Deselects all the selected options.

*All the deselect methods are applicable only if the multiple selections are allowed.
Here is an example code for drop-down =>

package seleniumexamples; import java.util.List; import org.openqa.selenium.By; import org.openqa.selenium.WebDriver; import org.openqa.selenium.WebElement; import; import; public class HandlingDropDowns { public static void main(String[] args) throws InterruptedException { // TODO Auto-generated method stub WebDriver driver; String baseURL=""; //Launch the web browser System.setProperty("","..\\SeleniumJava\\drivers\\chromedriver99.exe"); driver=new ChromeDriver(); //To maximize the size of window driver.manage().window().maximize(); //Navigate through an URL driver.get(baseURL); //Locate the drop down Select OldStyleSelectMenu=new Select(driver.findElement( ("oldSelectMenu"))); //Select values from drop down by visible text,by index and by value respectively OldStyleSelectMenu.selectByVisibleText("Aqua"); Thread.sleep(1500); OldStyleSelectMenu.selectByIndex(5); Thread.sleep(1500); OldStyleSelectMenu.selectByValue("red"); Thread.sleep(1500); //To get all the options of dropdown and print List <WebElement> options=OldStyleSelectMenu.getOptions(); System.out.println("All the options in this dropdown are : "); for(WebElement option:options) System.out.println(option.getText()); //Method to verify if the element is multi-select or not if(OldStyleSelectMenu.isMultiple()) System.out.println("\nThis dropdown supports multi select"); else System.out.println("\nDoesn't support multiselect"); //Close the current window driver.close(); } }

Output :

All the options in this dropdown are : Red Blue Green Yellow Purple Black White Voilet Indigo Magenta Aqua *Doesn't support multiselect ...

Refer next page WebDriver-Drag And Drop
bottom of page