Back to Top

Handling Drop Down List with Selenium WebDriver

Updated 28 February 2020

Drop Down And Multi-Select list is the set of HTML fields that we commonly interact with.So, In order to handle drop-down and multi-select list with Selenium WebDriver, it is essential to use Select class, It is the Webdriver class which is the part of the selenium package, provides the implementation of the HTML SELECT tag.

We need to Import the below package.

org.openqa.selenium.support.ui.Select

Select is an ordinary class, so it’s object is also created by a New keyword with regular class creation syntax.
Standard syntax of Select Class is as:-

Select dropdown = new Select(<WebElement>);

For a better understanding on using the <Select> class, refer at the below Java code.

WebElement myElement = driver.findElement(By.name("dropdown"));
Select dropdown = new Select(myElement);

// as an alternative, you can shorten your same code as

Select dropdown = new Select(driver.findElement(By.id("dropdown")));

WebDriver provides three ways to select an option from the drop-down menu. So, once you have initialized the <Select> object then, you can access all the methods used on the drop-down and Multi-Select list.

Start your headless eCommerce
now.
Find out More

Types of Select Methods:

  1. selectByVisibleText Method
  2. selectByIndex Method
  3. selectByValue Method

Here’s the illustration of the dropdown operation with following HTML Sample Code.

<html>
    <head>
            <title>Select Example</title>
        </head>
        <body>
                   <select name="dropdown">
                        <option >Select...</option>
                        <option value="SE">Selenium</option>
                        <option value="KT">KatalonStudio</option>
                        <option value="QTP">QTP</option>
                        <option value="AP">Appium</option>
                        <option value="JM">jMeter</option>
                    </select>
                </body>
    </html>

SelectByVisibleText Method

Selects options that match the input text in the argument, it will match the visible text in the dropdown field.

SYNTAX:-

dropdown.selectByVisibleText();

Here’s the Sample code to select the value based on the visible text.

package testpackage;package testpackage;
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 SelectMethod {
 
public static void main(String[] args) throws InterruptedException{
System.setProperty("webdriver.chrome.driver","/home/users/priya.singh/Downloads/chromedriver");

//Create a instance of ChromeDriver constructor

 WebDriver driver = new ChromeDriver();
 driver.manage().window().maximize();

//Give the navigation of the page where you want to select value for dropdown

 driver.get("https://www.path-to-select-dropdown.com/");

 Thread.sleep(10000);

 WebElement myElement = driver.findElement(By.name("dropdown"));
 Select dropdown= new Select(myElement);
 
//selectByVisibleText
 dropdown.selectByVisibleText("Selenium");
 }
}

selectByIndex Method

Selects the option based on the index value.

SYNTAX:-

dropdown.selectByIndex(Index);

Here’s the Sample code to select the value based on the Index value.

package testpackage;
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 SelectMethod {
 
public static void main(String[] args) throws InterruptedException{
System.setProperty("webdriver.chrome.driver","/home/users/priya.singh/Downloads/chromedriver");

//Create a instance of ChromeDriver constructor

 WebDriver driver = new ChromeDriver();
 driver.manage().window().maximize();

//Give the navigation of the page where you want to select value for dropdown

 driver.get("https://www.path-to-select-dropdown.com/");

 Thread.sleep(10000);

 WebElement myElement = driver.findElement(By.name("dropdown"));
 Select dropdown= new Select(myElement);
 
//selectByIndex
 dropdown.selectByIndex(2); // value is KatalonStudio
 }
}

selectByValue Method

Selects the option whose value attribute provided by the user in the specified parameter.

SYNTAX:-

dropdown.selectByValue(Value);

Here’s the Sample code to select the value based on the Value.

package testpackage;
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 SelectMethod {
 
public static void main(String[] args) throws InterruptedException{
System.setProperty("webdriver.chrome.driver","/home/users/priya.singh/Downloads/chromedriver");

//Create a instance of ChromeDriver constructor

 WebDriver driver = new ChromeDriver();
 driver.manage().window().maximize();

//Give the navigation of the page where you want to select value for dropdown

 driver.get("https://www.path-to-select-dropdown.com/");

 Thread.sleep(10000);

 WebElement myElement = driver.findElement(By.name("dropdown"));
 Select dropdown= new Select(myElement);
 
//selectByValue
 dropdown.selectByValue("SE"); // value is Selenium
 }
}

This is all about Handling Drop-Down list with Selenium WebDriver.

Thanks for reading this blog 🙂

Happy testing!!

. . .

Leave a Comment

Your email address will not be published. Required fields are marked*


Be the first to comment.

Back to Top

Message Sent!

If you have more details or questions, you can reply to the received confirmation email.

Back to Home