Skip to content
CodeKruPro

Select Dropdown Option by Visible Text

26/70

Video walkthrough

Watch on YouTube

Select Dropdown by Visible Text

Selenium provides the Select class (from org.openqa.selenium.support.ui.Select) to interact with HTML <select> elements.

selectByVisibleText() selects an option based on what the user sees.

HTML Structure:

<select id="singleSelect">
  <option value="" disabled selected>Select Language</option>
  <option value="java">Java</option>
  <option value="python">Python</option>
  <option value="javascript">JavaScript</option>
  <option value="csharp">C#</option>
  <option value="ruby">Ruby</option>
</select>

Task:

  1. Navigate to https://testkru.com/Elements/Dropdowns
  2. Create a Select object for the dropdown with ID singleSelect
  3. Select "Java" by visible text
  4. Print the selected option's text

Expected output:

Selected: Java

About this lesson

Select a dropdown option with selectByVisibleText in Selenium Java

Selenium Java AssignmentsIntermediateDropdowns

How it works

Native <select> dropdowns are handled with the Select class from org.openqa.selenium.support.ui. You wrap the select element, new Select(element), and then choose options by text, value or index instead of clicking the list open.

selectByVisibleText() matches the text the user sees in the option. It is the most readable choice because the test reads like the screen. The match is exact and case-sensitive: asking for a lower-case version of an option throws NoSuchElementException with "Cannot locate option with text".

To read the choice back, getFirstSelectedOption() returns the selected option as a WebElement. Before anything is chosen, it returns the placeholder: on the Dropdowns page that is the disabled "Select Language" option, which loads selected.

About the assignment

The singleSelect dropdown lists five languages under a placeholder. You wrap it in a Select, choose one language by its visible text and read the selected option back, so you see the full select-then-verify loop.

What you will do

  1. Find the dropdown by its id and pass it to new Select(). The import is already in the starter.
  2. Call selectByVisibleText() with the language from the task, written exactly as it appears in the list.
  3. Get the selected option with getFirstSelectedOption(), read its text and print it after the label Selected:.

Common mistakes

  • Wrapping something that is not a select

    new Select() checks the tag. Passing any other element throws UnexpectedTagNameException, for example "Element should have been "select" but was "div"". Styled dropdowns built from divs need clicks instead.

  • Choosing in a disabled dropdown

    The page's disabledDropdown throws UnsupportedOperationException: You may not select an option in disabled select.

  • Calling getText() on the select

    That returns every option's text on separate lines, not the chosen one. Read the selected option instead.

In a real test suite

Country, currency and language pickers are typical native dropdowns. Selecting by visible text keeps tests readable, but it breaks when labels are translated, which is when teams switch to selectByValue().

Lesson 25 checked disabled state. The next two lessons choose from this same dropdown by value and by position.

To try one of these calls on any page without a task, open the online Selenium compiler for Java.

Starter code

The editor above opens with this code. Fill in the TODO lines, then run and submit it.

Main.java
import org.openqa.selenium.*;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.support.ui.Select;

public class Main {
    public static void main(String[] args) throws Exception {
        WebDriver driver = new ChromeDriver();
        driver.get("https://testkru.com/Elements/Dropdowns");
        
        // TODO: Create Select object for singleSelect dropdown
        // TODO: Select "Java" by visible text
        // TODO: Print the selected option text
        
        driver.quit();
    }
}