New data from 1,500+ QA pros: The 2025 State of Software Quality Report is live
DOWNLOAD YOUR COPY
All All News Products Insights AI DevOps and CI/CD Community

How to Easily Handle Dropdowns in Selenium with the Select Class

Learn how to handle dropdown menus in Selenium using Select class, with clear examples and code snippets. Step-by-step examples to boost test reliability and speed.

Hero Banner
Smart Summary

Effectively managing dropdown menus in web automation is fundamental for building reliable test suites. We detail how to leverage Selenium's robust Select class and its specific methods to precisely interact with these elements, while also showcasing how Katalon Studio significantly accelerates this process through built-in keywords and object capture, minimizing boilerplate code.

  • Utilize Selenium's Select Class for HTML Dropdowns: The Select class is indispensable for interacting with <select> HTML elements in Selenium, offering methods like selectByIndex(), selectByVisibleText(), and selectByValue() to accurately choose options within your test scripts.
  • Choose the Right Selection Method Wisely: Opt for selectByVisibleText() when targeting user-displayed text, selectByValue() for attribute-based stability, and use selectByIndex() cautiously due to potential brittleness if the option order shifts, while deselectAll() specifically handles multi-select scenarios.
  • Streamline Dropdown Automation with Katalon Studio: Leverage Katalon Studio's Spy Web to capture dropdown elements and utilize built-in keywords like WebUI.selectOptionByValue for efficient, low-code automation, significantly reducing boilerplate code and simplifying test maintenance.
Good response
Bad response
|
Copied
>
Read more
Blog / Insights /
How to Easily Handle Dropdowns in Selenium with the Select Class

How to Easily Handle Dropdowns in Selenium with the Select Class

Technical Writer, Katalon Updated on

In this article, we’ll quickly cover those core Selenium commands and then show how Katalon Studio streamlines the same process - letting you capture a dropdown with Spy Web and select options using built-in keywords like selectOptionByValue or selectOptionByIndex, so you can automate dropdown interactions without writing boilerplate code, but first...

What is dropdown in Selenium?

In web application, a dropdown menu (also called a dropdown list) is a graphical control element that lets users pick one option from a list. When inactive, it shows a single selected value. When clicked, it expands to display all available options. A dropdown is presented as follows in HTML:

Copy

<!DOCTYPE html>
<html>
  <head>
    <meta charset="UTF-8" />
    <title>Select Element Example</title>
  </head>
  <body>
    <h1>The select element</h1>
    <p>The select element is used to create a drop-down list.</p>

    <form action="/action_page.php">
      <label for="cars">Choose a car:</label>
      <select name="cars" id="cars">
        <option value="volvo">Volvo</option>
        <option value="saab">Saab</option>
        <option value="opel">Opel</option>
        <option value="audi">Audi</option>
      </select>
      <br /><br />
      <input type="submit" value="Submit" />
    </form>

    <p>
      Click the "Submit" button and the form-data will be sent to a page on
      the server called "action_page.php".
    </p>
  </body>
</html>

What is Select class in Selenium?

The Select class provides convenient methods to interact with <select> HTML elements. You must first locate the dropdown element, then use Select to select or deselect items. 

Common <select> class methods

1. selectByIndex()

Select the option at the given index. the Index starts from `0`.

Example Java code:
Copy

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

What it does: This code selects the option at index 2, which corresponds to the third option in the list.

2. selectByVisibleText()

Select all options that display text matching the argument.

Example Java code:

Copy

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

What it does: The code finds the dropdown by its ID, wraps it with the `Select` class, and selects the option with displayed text matching "Audi".

3. selectByValue()

Select all options that have a value matching the argument.

Example Java code:

Copy

Select dropdown = new Select(driver.findElement([By.id]
dropdown.selectByValue("saab");

What it does: The code selects the option with value attribute as "saab".

4. deselectAll()

Clear all selected items. Note that this method only works if the dropdown allows a multi-select dropdown.

Example Java code:

Copy

 dropdown.deselectAll();

Handle dropdown with Katalon Studio

Katalon Studio provides built-in keywords for the Select class that simplify dropdown handling and automate your testing workflow. 

Let’s give it a try! First we’ll need to capture some objects that we can use in the test case.

📄 Learn more in our documentation: How to handle dropdown menu in Katalon Studio

Capture objects with Spy Web

Step 1. In your test project, open the Spy Web tool.

Screenshot 2025-05-19 at 14.03.48

Step 2. Provide a link in the URL field. Here we’re using the https://www.w3schools.com/tags/tryit.asp?filename=tryhtml_select site that contains a drop-down list.

Step 3. Click Start to launch your session. Navigate your cursor to the dropdown and right-click > Capture Object.

How to capture drop down object on a web page using Katalon Studio

The object we captured is a <select> HTML element, and its XPath attributes are displayed in the Object Spy dialog. 
Capturing a <select> HTML element by its XPath in Katalon Studio
Click Save > OK in the Add Element to Object Repository dialog.How to add captured Element to the Object Repository in Katalon Studio

Use Katalon built-in keywords

With the objects you have captured, you can start creating test steps with Katalon Studio built-in keywords.

Open your test case in the Manual view, create a test case using the WebUI.selectOptionByValue keyword that finds the value="saab”

Using Katalon built-in keywords

You can test it out with this script:

Copy

WebUI.*openBrowser*('')

WebUI.*navigateToUrl*('https://www.w3schools.com/tags/tryit.asp?filename=tryhtml_select')

WebUI.*selectOptionByValue*(*findTestObject*('Object Repository/Page_W3Schools/select_Cars'),

'saab', **true**)

WebUI.*delay*(3)

WebUI.*closeBrowser*()

If you want to use another keyword, for example WebUI.selectOptionByIndex, try:

Copy
WebUI.selectOptionByIndex(findTestObject('Object Repository/Page_W3Schools/select_Cars'), 2, FailureHandling.STOP_ON_FAILURE)

Conclusion

Handling dropdowns is straightforward with Selenium’s Select class - just locate the <select> element and call methods like selectByIndex, selectByVisibleText, or selectByValue - but maintaining those scripts can become tedious over time. Katalon Studio streamlines the same workflow by letting you capture the dropdown with Spy Web and then call keywords like selectOptionByValue or selectOptionByIndex without writing extra boilerplate. Whether you prefer full control through Selenium code or a faster, low-code approach in Katalon, both paths ensure your dropdown interactions stay reliable and easy to maintain.

upgrade-from-selenium-to-katalon-studio

Frequently asked questions

What is a dropdown menu in web automation testing?

+

A dropdown menu (or dropdown list) is an HTML <select> element that allows users to choose one (or multiple) options from a hidden list. In automation, you interact with dropdowns by locating the <select> element and choosing one of its <option> children, rather than clicking each option manually.

How does Selenium’s Select class simplify dropdown handling?

+

Selenium’s Select class wraps any <select> element and exposes methods like selectByIndex(int index), selectByVisibleText(String text), and selectByValue(String value). By using these methods, you can reliably choose the desired option without writing low-level click or JavaScript code to open and pick from the list.

When should I use selectByVisibleText() vs. selectByValue() vs. selectByIndex()?

+

 

  • selectByVisibleText("Label") is best when you know the exact text shown to users (e.g., “Audi”).

  • selectByValue("valueAttr") works when you know the <option value="..."> attribute (e.g., value=“saab”), which can be more stable if display text changes.

  • selectByIndex(2) selects the option at that zero-based position. It’s quick but brittle—if the order of options shifts, your index can point to the wrong choice.

 

How do I handle a multi-select dropdown in Selenium?

+

For <select multiple="true"> elements, you can use Select methods like selectByVisibleText(), selectByValue(), or selectByIndex() multiple times to pick more than one option. To clear all selected items, call deselectAll(). Remember, deselect methods only work on multi-select dropdowns.

How does Katalon Studio streamline dropdown automation compared to raw Selenium code?

+

In Katalon Studio, you capture the <select> element once with Spy Web, store it as a Test Object, and then call built-in keywords like WebUI.selectOptionByValue(testObject, "saab", true) or WebUI.selectOptionByIndex(testObject, 2). This eliminates boilerplate code—no need to instantiate a Select object in script. Katalon handles locator stability and waits automatically, so you can write fewer lines and maintain tests more easily.


Ask ChatGPT
|
Trinh Huynh
Trinh Huynh
Technical Writer, Katalon
Trinh Huynh is an experienced technical writer in the software testing industry. She blends her extensive technical knowledge with deep understanding of the Katalon products to create guides that help testers of all levels.
on this page
Click