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
- Call
selectByValue()on theSelectwith the value from the task. - Get the selected option with
getFirstSelectedOption(). - 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 thevalueattributes 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()returnsvoid. Read the choice back withgetFirstSelectedOption().
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.
Related lessons
- Select Dropdown Option by Visible TextChoose by the text the user sees instead.
- Get First Selected OptionRead both the text and the value of a selected option.
- Get Attribute Value with getAttribute()How
getAttribute("value")reads an element's value.
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.
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();
}
}