Skip to content
CodeKruPro

Select Dropdown by Value with selectByValue()

27/70

Video walkthrough

Watch on YouTube

Select Dropdown by Value

selectByValue() selects an option using its value attribute (which may differ from the visible text).

On the Dropdowns page, the singleSelect has options like:

  • <option value="python">Python</option>

Task:

  1. Navigate to https://testkru.com/Elements/Dropdowns
  2. Select the option with value python
  3. Print the displayed text of the selected option

Expected output:

Selected: Python

About this lesson

selectByValue in Selenium Java: choose a dropdown option by its value attribute

Selenium Java AssignmentsIntermediateDropdowns

How it works

Every option has a value that the form sends to the server, and it often differs from the text the user sees. In the singleSelect dropdown the values are lower-case ids such as csharp for the option shown as "C#". selectByValue() chooses by that hidden value.

Values tend to be more stable than labels. Copy changes and translations alter the visible text, while the value stays the same because the back end depends on it. That makes selectByValue() a good default for tests that run in several languages.

Matching is exact and case-sensitive. A value with a capital letter that the page writes in lower case throws NoSuchElementException with "Cannot locate option with value".

About the assignment

The starter already wraps singleSelect in a Select. You choose an option by its value and then print the text the user would see for it, which shows the difference between what the form sends and what the page displays.

What you will do

  1. Call selectByValue() on the Select with the value from the task.
  2. Get the selected option with getFirstSelectedOption().
  3. Print its visible text, not its value, after the label Selected:.

Common mistakes

  • Passing the visible text as the value

    The values here are lower case, the labels are not. Passing the label throws NoSuchElementException. Look at the value attributes in the page source first.

  • Printing the value instead of the text

    getAttribute("value") on the selected option prints the lower-case value, so the output does not match the expected line.

  • Expecting a return value

    selectByValue() returns void. Read the choice back with getFirstSelectedOption().

In a real test suite

API-backed forms often have values that are ids or codes, such as US for "United States". Selecting by those codes keeps tests independent of the display text and lines them up with what the server receives.

Lesson 26 chose by visible text. Lesson 28 chooses by position, which is useful when you do not know the text in advance.

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");
        
        Select dropdown = new Select(driver.findElement(By.id("singleSelect")));
        
        // TODO: Select by value "python"
        // TODO: Print the selected option's visible text
        
        driver.quit();
    }
}