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
- Find the dropdown by its id and pass it to
new Select(). The import is already in the starter. - Call
selectByVisibleText()with the language from the task, written exactly as it appears in the list. - Get the selected option with
getFirstSelectedOption(), read its text and print it after the labelSelected:.
Common mistakes
Wrapping something that is not a select
new Select()checks the tag. Passing any other element throwsUnexpectedTagNameException, 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
disabledDropdownthrowsUnsupportedOperationException: 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.
Related lessons
- Select Dropdown by Value with selectByValue()Choose by the
valueattribute, which does not change with translations. - Select Dropdown by Index with selectByIndex()Choose by position in the list.
- Get First Selected OptionRead the option that loads already selected.
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");
// TODO: Create Select object for singleSelect dropdown
// TODO: Select "Java" by visible text
// TODO: Print the selected option text
driver.quit();
}
}