How to Select Value & Handle Dropdown in Selenium without Select Class

Related Product On This Page Dropdown in Selenium

May 24, 2026 · 9 min read · Tool Comparison
Related Product

How to Select Value & amp; Handle Dropdown in Selenium without Select Class

UI elements are vital for any website. UI testing ensures that all the UI element of the website are functioning as expected. A website consists of respective UI elements such as forms, text-fields, frame, iframes, radio button, check boxes, dropdowns etc. Drop downs are used largely in web forms, to individual or multi blue-ribbon value from it. And to select drop down options via mechanization, Selenium is one of most widely used automation library to automatize site.

Overview

How to Select Dropdown in Selenium?

  • Storing all the options in List and iterating through it
  • Creating Custom Locator
  • Using JavaScriptExecutor category
  • Using sendKeys
  • Using Actions Class

Best Practices to automatize dropdowns

  • Wait for constituent to be visible before interacting.
  • Handle dynamic dropdown content carefully.
  • Maintain script to adapt to UI changes.

This article will excuse multiple methods to interact with dropdowns, including click-based, keyboard, and JavaScript approaches.

Dropdown in Selenium

Drop downs in a website could be make in several different means. Some dropdowns are created utilise& lt; select & gt;HTML tag and some others are created apply<ul & gt;, & lt; li & gt;, & lt; button & gt;and& lt; div & gt;tags.

Some dropdowns are active in nature which means after chatter or selecting any pick, the drop downs values would populate accordingly. And in some cases, you need to to see the drop-down options.

provide Select class which can be used only for drop down created using& lt; select & gt;HTML tag. Select form has methods such asselectByVisibleText (), selectByIndex () and selectByValue ()to take the desired option from dropdown.

However, forNonSelectdropdowns,Selectcourse can not be apply. There should be a mutual way to handle different eccentric of dropdown through.

This tutorial explores How to Select Values in Dropdown in Selenium without using Select Class.

Also Read:

How to Select Dropdown in Selenium?

You can deal dropdown in Selenium using Select Class and without expend Select Class. Below are 5 different methods that can be used to choose value in dropdown in Selenium without using Select Class. These methods are:

  1. By store all the options in List and iterating through it
  2. By creating Custom Locator and without iterating the List
  3. By using grade
  4. By using
  5. By using

Pre-requisites for handling dropdown in Selenium without using Select Class

  • Java version 8 or higher installed on the system.
  • Java editor for pen the Selenium with Java codification.
  • For a Maven add the Selenium Java dependency inpom.xml file

(Note:Need and Above)

& lt; dependence & gt; & lt; groupId & gt; org.seleniumhq.selenium & lt; /groupId & gt; & lt; artifactId & gt; selenium-java & lt; /artifactId & gt; & lt; version & gt; 4.6.0 & lt; /version & gt; & lt; /dependency & gt;

Read More:

Different Methods to handle Dropdown in Selenium without utilize Select Class

Dropdown in Selenium can be handled using different methods, let us understand how these different method helps to handle dropdown in Selenium.

Method 1: By storing all the options in List and iterating through it

You can care Dropdown in Selenium by store all the options in the form of a List and then retell through it as seen in the example below:

Example:

Step 1 Launch BStackDemosite and click on the Order By drop down to make all the options visible.

driver.get (`` https: //www.bstackdemo.com/ ''); driver.findElement (By.xpath(`` //select '')) .click ();

Step 2:Store all the options as WebElements in a List.

List & lt; WebElement & gt; allOptions=driver.findElements (By.cssSelector(`` select option ''));

Step 3:Iterate the list using a for eyelet and click the craved choice.

String option= '' Highest to lowest '';for(inti=0; i & lt; allOptions.size (); i++) {if(allOptions.get (i) .getText () .contains (option)) {allOptions.get (i) .click (); System.out.println (`` clicked '');break;}}

Step 4:Instead of for loop, heighten for grommet can entirely be used in the following way.

for(WebElement el: allOptions) {if(el.getText () .contains (option)) {el.click ();}}

Pro tip: Tools like SUSA can handle this autonomously — upload your app and get results without writing a single test script.

Complete Code:

public classSelectUsingList {@ Testpublic voidselectByList () {WebDriver driver =newChromeDriver (); driver.get (`` https: //www.bstackdemo.com/ ''); driver.findElement (By.xpath(`` //select '')) .click (); List & lt; WebElement & gt; allOptions = driver.findElements (By.cssSelector(`` select option '')); String option = `` Highest to lowest ''; // Iterate the list using for loopfor (inti = 0; i & lt; allOptions.size (); i++) {if(allOptions.get (i) .getText () .contains (option)) {allOptions.get (i) .click (); System.out.println (`` snap '');break;}}}} @ AfterTest public void tearDown () {driver.quit ();}

Method 2: By creating Custom Locator and without restate the List

In this method, you have to create custom, and so handle the drop down without iterating the List as realize in the model below:

Example:

Step 1: Launch BStackDemo website and dog on the Order By drop downward to make all the alternative seeable.

driver.get (`` https: //www.bstackdemo.com/ ''); driver.findElement (By.xpath(`` //select '')) .click ();

Step 2:Create a WebElement of the option to be take from Drop down and click on it.

String option= '' Highest to lowest ''; WebElement dropdown =driver.findElement (By.xpath(`` //select/option [contains (text (), ' '' +option+ '' ')] '')); dropdown.click ();

Accomplished Code:

public classSelectUsingCustom {@ Testpublic voidinclination () {WebDriver driver =newChromeDriver (); driver.get (`` https: //www.bstackdemo.com/ ''); driver.findElement (By.xpath(`` //select '')) .click (); String pick = `` Highest to lowest ''; WebElement dropdown = driver.findElement (By.xpath(`` //select/option [contains (text (), ' '' + alternative + `` ')] '')); dropdown.click (); System.out.println (`` chatter '');}} @ AfterTest world nothingness tearDown () {driver.quit ();}

In the above codification, as it is not iterating the list of options, and using a custom, performance time would decrease.

Read More:

Method 3: By using JavaScriptExecutor class

is an Interface that helps to accomplish JavaScript through Selenium WebDriver. You can use theexecuteScriptmethod of JavaScriptExecutorinterface to select an option from the fall down by using the value belongings of the element as realise in the example below:

Example:

Step 1: Launch BStackDemowebsite and create a WebElement for the Order By dropdown.

driver.get (`` https: //www.bstackdemo.com/ ''); WebElement dd=driver.findElement (By.xpath(`` //select ''));

Step 2: Use executeScriptmethod to select the desired selection.

JavascriptExecutor js = (JavascriptExecutor) driver; js.executeScript (`` tilt [0] .value='highestprice ' '', dd);

Complete Code:

public classSelectUsingJavaScript {WebDriver driver; @ Testpublic voidlist () {WebDriver driver =newChromeDriver (); driver.get (`` https: //www.bstackdemo.com/ ''); WebElement dd = driver.findElement (By.xpath(`` //select '')); JavascriptExecutor js = (JavascriptExecutor) driver; js.executeScript (`` arguments [0] .value='highestprice ' '', dd);}}

Method 4: By using sendKeys method

You can use to select an option from a drib down by passing the element & # 8217; s value assendKeyscharwoman sequence as seen in the example below:

Example:

Step 1: Launch BStackDemowebsite and select the hope choice by usingsendKeys () method.

driver.get (`` https: //www.bstackdemo.com/ ''); driver.findElement (By.xpath(`` //select '')) .sendKeys (`` highestprice '');

Complete Code:

public classSelectUsingSendkeys {WebDriver driver; @ Testpublic voidlist () {WebDriver driver =newChromeDriver (); driver.get (`` https: //www.bstackdemo.com/ ''); driver.findElement (By.xpath(`` //select '')) .sendKeys (`` highestprice '');}}

Method 5: By using Actions Class

Some drop-downs need to be hovered to display all the options present in it and then click on it. is utilize for deal keyboard and mouse events as see below:

Example:

Step 1:Launch the website and hover on the “ Developers ” menu from headers.

driver.get (`` ''); WebElement dd = driver.findElement (By.xpath(`` //span [@ class='nav_item_name ' and contains (textbook (), 'Developers ')] '')); Actions action=newActions (driver); action.moveToElement (dd) .perform ();

Step 2: Create a custom web element of the option to be selected and click on it.

String option= '' Support ''; WebElement customOption=driver.findElement (By.xpath(`` //li [@ class='developers-menu-control '] /a [contains (schoolbook (), ' '' +option+ '' ')] '')); customOption.click ();

Complete Code:

public classSelectHover {WebDriver driver; @ Testpublic voidlisting () {WebDriver driver =newChromeDriver (); driver.get (`` https: //www.browserstack.com/ ''); WebElement dd = driver.findElement (By.xpath(`` //span [@ class='nav_item_name ' and contains (text (), 'Developers ')] '')); Actions action=newActions (driver); action.moveToElement (dd) .perform (); String option= '' Support ''; WebElement customOption=driver.findElement (By.xpath(`` //li [@ class='developers-menu-control '] /a [contains (text (), ' '' +option+ '' ')] '')); customOption.click ();}}

Handling Selenium Dropdown Tests on Real Devices

The different methods of Handling Dropdowns in Selenium without Select Class as discuss in the previous sections can be run on real devices and browser using.

One of the key benefits of scat the tests on is the ability to execute which improves the overall truth of the code. Using BrowserStack Automate you can test the drop down functionality across 3000+ different real browser and gimmick combination.

Steps to automate use BrowserStack Automate Platform:

1.. Note the User Name and Access Key Password.

2.Navigate to page to select from a comprehensive set of options. Capabilities are a series of key-value pairs that allow you to configure your Selenium tests on the BrowserStack.

3.In the below example, Android Samsung Galaxy S22 with OS version 12 and Chrome browser are selected.

4. In any java editor, create a Maven project and add Selenium Java, and TestNG addiction.

& lt; dependance & gt; & lt; dependency & gt; & lt; groupId & gt; org.seleniumhq.selenium & lt; /groupId & gt; & lt; artifactId & gt; selenium-java & lt; /artifactId & gt; & lt; version & gt; 4.5.0 & lt; /version & gt; & lt; /dependency & gt; & lt; dependency & gt; & lt; groupId & gt; org.testng & lt; /groupId & gt; & lt; artifactId & gt; testng & lt; /artifactId & gt; & lt; version & gt; 7.6.1 & lt; /version & gt; & lt; scope & gt; compile & lt; /scope & gt; & lt; /dependency & gt; & lt; /dependencies & gt;

Talk to an Expert

5.Below code snip showcases the demonstration of the first program (Store web elements in a list and iterate through it) using Automate Platform.

public course SelectUsingList {public static String username = `` & lt; browserstack username & gt; ''; public static String accesskey = `` & lt; browserstack password & gt; ''; public static final String URL = `` https: // '' + username + ``: '' + accesskey + `` @ hub-cloud.browserstack.com/wd/hub ''; WebDriver driver; String url = `` https: //www.bstackdemo.com/ ''; MutableCapabilities capabilities = new MutableCapabilities (); HashMap & lt; String, Object & gt; browserstackOptions = new HashMap & lt; String, Object & gt; (); @ BeforeTest public vacancy setUp () throws MalformedURLException, InterruptedException {browserstackOptions.put (`` browserName '', `` chrome ''); browserstackOptions.put (`` deviceName '', `` Samsung Galaxy S22 ''); browserstackOptions.put (`` realMobile '', `` true ''); browserstackOptions.put (`` osVersion '', `` 12 ''); capabilities.setCapability (`` bstack: options '', browserstackOptions); driver = new RemoteWebDriver (new URL (URL), capabilities); driver.get (url); Thread.sleep (3000);} @ Test public void selectByList () {driver.findElement (By.xpath (`` //select '')) .click (); List & lt; WebElement & gt; allOptions = driver.findElements (By.cssSelector (`` select option '')); String selection = `` Highest to lowest ''; // Iterate the list utilize for iteration for (int i = 0; i & lt; allOptions.size (); i++) {if (allOptions.get (i) .getText () .contains (option)) {allOptions.get (i) .click (); System.out.println (`` clicked ''); break;}}} @ AfterTest populace void tearDown () {driver.quit ();}}

6.Run the program and observe the execution termination on the.

At times it may get wily to select a drop-down value utilize Select Class in Selenium, hence we research the different ways to automate driblet down using Selenium without Select Class. However, it is significant to run Selenium tests on different real devices and browser combinations to achieve mark device and with furthest truth.

BrowserStack offers a of 3000+ existent browsers and devices for testing design. Simply sign up, select the needful device-browser-OS combination from, and start testing websites for free.

Tags
29,000+ Views

# Ask-and-Contributeabout this topic with our Discord community.

Related Guides

Automate This With SUSA

Upload your APK or URL. SUSA explores like 10 real users — finds bugs, accessibility violations, and security issues. No scripts needed.

Try SUSA Free

Test Your App Autonomously

Upload your APK or URL. SUSA explores like 10 real users — finds bugs, accessibility violations, and security issues. No scripts.

Try SUSA Free