About this lesson
getFirstSelectedOption in Selenium Java: read a dropdown's current choice
Learn Selenium JavaIntermediateDropdowns
How it works
getFirstSelectedOption() returns the selected option of a dropdown as a WebElement. On a single dropdown that is the one current choice, so it is the method to use when a test needs to know what a dropdown shows right now, whether the test chose it or the page loaded with it.
Because you get the whole option element, you can read both sides of it: getText() for the label the user sees and getAttribute("value") for what the form will send. The two often differ in case or wording.
On the Dropdowns page preSelectedDropdown loads with one option marked selected in the HTML. getAllSelectedOptions() on it returns a list of one, and on a multi-select with nothing chosen, getFirstSelectedOption() throws NoSuchElementException instead of returning null.
About the assignment
The starter wraps preSelectedDropdown, a list of four web technologies with one chosen by the page. Without selecting anything, you read which option is selected and print both its visible text and its value.
What you will do
- Call
getFirstSelectedOption()and keep the returnedWebElement. - Print its
getText()after the labelSelected text:. - Print its
valueattribute after the labelSelected value:.
Common mistakes
Selecting something first
The task is to read the page's default. Any
selectBy...call before reading changes the answer.Reading the value from the wrong element
getAttribute("value")on the option and on theselectboth return the selected value here, but the option is the element you should check, because it also gives you the text.Expecting null when nothing is selected
An empty multi-select throws
NoSuchElementException. UsegetAllSelectedOptions().isEmpty()to check for no selection safely.
In a real test suite
Edit forms load with saved choices. Asserting the pre-selected option is how a test checks that a saved setting was loaded correctly before it changes anything.
This lesson ends the dropdown lessons. Lesson 32 starts on element state checks with isDisplayed().
Related lessons
- Select Dropdown Option by Visible TextChoose an option first, then read it back the same way.
- Select Dropdown by Value with selectByValue()Values and visible texts, and which one to select by.
- Handle Pre-Selected CheckboxesCheckboxes also load with a default state you should read first.
To try one of these calls on any page without a task, open the Online Selenium Java Compiler.
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("preSelectedDropdown")));
// TODO: Get the first selected option
// TODO: Print its text and value attribute
driver.quit();
}
}