Skip to content
CodeKruPro

Get First Selected Option

31/70

Get First Selected Option

getFirstSelectedOption() returns the currently selected option element.

On the Dropdowns page, dropdown #5 (id="preSelectedDropdown") has "JavaScript" pre-selected.

HTML Structure:

<select id="preSelectedDropdown">
  <option value="html">HTML</option>
  <option value="css">CSS</option>
  <option value="javascript" selected>JavaScript</option>
  <option value="typescript">TypeScript</option>
</select>

Note the selected attribute on the JavaScript option.

Task:

  1. Navigate to https://testkru.com/Elements/Dropdowns
  2. Get the first selected option from the preSelectedDropdown
  3. Print its text and value

Expected output:

Selected text: JavaScript
Selected value: javascript

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

  1. Call getFirstSelectedOption() and keep the returned WebElement.
  2. Print its getText() after the label Selected text:.
  3. Print its value attribute after the label Selected 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 the select both 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. Use getAllSelectedOptions().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().

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.

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("preSelectedDropdown")));
        
        // TODO: Get the first selected option
        // TODO: Print its text and value attribute
        
        driver.quit();
    }
}