i

Selenium Step By Step Guide

Handling Dropdowns and Select class

The drop-down is nothing but a list of elements grouped in a single tab. When we deal with handling drop-downs, we must look at the group of the elements represent the drop-down tab on a web page. We need to find the web element of Drop-down using various locators like id, className, name, and XPath, etc. Once we get the Drop-down element, we use the "Select" class by importing it from ‘import org.openqa.selenium.support.ui.Select' package.

Select class is a selenium class which provides a set of methods to work on elements surrounded by a Select tag in HTML source code. One should take care that if and only if the drop-down element stays inside a Select tag, then only this Select class concept works. If tag is not a Select tag then a tag name exception “org.openqa.selenium.support.ui.UnexpectedTagNameException” occurs.Select is a normal class, and we create an object in the same way we create using the common object creation syntax.

Select select=new Select();

Once we write the above syntax, we get an error asking us to provide a WebElement type object in parameter to Select() constructor. Let us provide the element object as a parameter inside Select class constructor.

WebElement element=driver.findElement(By.xpath(xpathExpression));

Select select=new Select(element);

Let us take an example to select and click a specific element present in drop down list. We have a drop-down having elements inside it and need to select an option say “Ember”

 

Below is the HTML code for the above drop-down window. HTML code comes once we click on the Inspect element option of the browser.

Create an object of Select class by providing a drop-down element object as a parameter for Select class constructor.

 WebElement element=driver.findElement(By.name(“skills”));

Select select=new Select(element);

Now when we have got the select object, we can get all the elements inside the drop down element using getOptions() method. We will collect all these elements in a List collection.

List options=select.getOptions();

We can check the number of elements inside the Drop-down list by using the size() method of a list object.

int iSize=options.size();

once we get the idea of the number of elements available inside the drop-down list, then we can easily select any specific element from the drop-down list by using a loop running for the size of elements.

Let us see the complete code to select the "Ember" element and get the text out of it and print the same on the console.

package SeleniumTest.SeleniumTest;

import java.util.List;

import org.openqa.selenium.By;

import org.openqa.selenium.WebDriver;

import org.openqa.selenium.WebElement;

import org.openqa.selenium.chrome.ChromeDriver;

import org.openqa.selenium.support.ui.Select;

public class DropDown

{

    public static void main( String[] args )

    {

        System.setProperty("webdriver.chrome.driver","C:\\Users\\chromedriver.exe");

        WebDriver driver=new ChromeDriver();

        driver.get("https://selflearning.io/study-material/html-css");

        WebElement element=driver.findElement(By.xpath("/html/body/header/div[4]/div/ul/li[1]"));

        Select select=new Select(element);

        List options=select.getOptions();

        int iSize=options.size();

        System.out.println(iSize);

        for(int i=0;i<iSize;i++)

        {

            if(options.get(i).getText().contains("Ember"))

            {

                  System.out.println(options.get(i).getText());

                  options.get(i).click();

                 

            }

           

        }

        String eleText=element1.getText();

        System.out.println(eleText);

        driver.quit();

    }

}