Bird
Raised Fist0
Selenium Javatesting~15 mins

Select by value, visible text, index in Selenium Java - Build an Automation Script

Choose your learning style10 modes available
Verify dropdown selection by value, visible text, and index
Preconditions (1)
Step 1: Locate the dropdown menu by id 'country-select'
Step 2: Select the option with value 'US' from the dropdown
Step 3: Verify that the selected option's value is 'US'
Step 4: Select the option with visible text 'Canada' from the dropdown
Step 5: Verify that the selected option's visible text is 'Canada'
Step 6: Select the option at index 3 from the dropdown
Step 7: Verify that the selected option is the one at index 3
✅ Expected Result: The dropdown selections by value, visible text, and index are successful and verified correctly
Automation Requirements - Selenium WebDriver with Java
Assertions Needed:
Verify selected option value after selecting by value
Verify selected option text after selecting by visible text
Verify selected option index after selecting by index
Best Practices:
Use explicit waits to ensure dropdown is visible before interacting
Use Selenium's Select class for dropdown interactions
Use meaningful assertions with clear messages
Use By.id locator for dropdown element
Avoid hardcoded waits like Thread.sleep
Automated Solution
Selenium Java
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.Select;
import org.openqa.selenium.support.ui.WebDriverWait;
import java.time.Duration;
import static org.junit.jupiter.api.Assertions.assertEquals;

public class DropdownSelectionTest {
    public static void main(String[] args) {
        System.setProperty("webdriver.chrome.driver", "path/to/chromedriver");
        WebDriver driver = new ChromeDriver();
        WebDriverWait wait = new WebDriverWait(driver, Duration.ofSeconds(10));

        try {
            driver.get("https://example.com/dropdownpage");

            // Wait until dropdown is visible
            WebElement dropdownElement = wait.until(
                ExpectedConditions.visibilityOfElementLocated(By.id("country-select"))
            );

            Select countrySelect = new Select(dropdownElement);

            // Select by value 'US'
            countrySelect.selectByValue("US");
            String selectedValue = countrySelect.getFirstSelectedOption().getAttribute("value");
            assertEquals("US", selectedValue, "Selected value should be 'US'");

            // Select by visible text 'Canada'
            countrySelect.selectByVisibleText("Canada");
            String selectedText = countrySelect.getFirstSelectedOption().getText();
            assertEquals("Canada", selectedText, "Selected text should be 'Canada'");

            // Select by index 3
            countrySelect.selectByIndex(3);
            String selectedTextByIndex = countrySelect.getFirstSelectedOption().getText();
            // Verify the option at index 3 is selected
            // Note: Index is zero-based, so index 3 means the 4th option
            // We verify by comparing the selected option's index
            int selectedIndex = -1;
            for (int i = 0; i < countrySelect.getOptions().size(); i++) {
                if (countrySelect.getOptions().get(i).equals(countrySelect.getFirstSelectedOption())) {
                    selectedIndex = i;
                    break;
                }
            }
            assertEquals(3, selectedIndex, "Selected index should be 3");

        } finally {
            driver.quit();
        }
    }
}

This test script uses Selenium WebDriver with Java to automate dropdown selection.

First, it opens the browser and navigates to the page with the dropdown.

It waits explicitly until the dropdown is visible to avoid timing issues.

Then it creates a Select object to interact with the dropdown.

It selects options by value, visible text, and index, verifying each selection with assertions.

The assertions check that the selected option matches the expected value or text.

Finally, it closes the browser to clean up.

Common Mistakes - 4 Pitfalls
Using Thread.sleep() instead of explicit waits
Using incorrect locator like XPath with hardcoded indexes
{'mistake': 'Not verifying the selected option after selection', 'why_bad': 'Without verification, the test might pass even if the selection failed.', 'correct_approach': "Use assertions to check the selected option's value or text after each selection."}
Mixing different frameworks or APIs in the same test
Bonus Challenge

Now add data-driven testing to select multiple countries by value, visible text, and index using a loop

Show Hint

Practice

(1/5)
1. In Selenium Java, which method of the Select class allows you to select an option from a dropdown by the exact text shown to the user?
easy
A. selectOptionByText(String text)
B. selectByValue(String value)
C. selectByIndex(int index)
D. selectByVisibleText(String text)

Solution

  1. Step 1: Understand Select class methods

    The Select class provides methods to select dropdown options by different criteria: visible text, value attribute, or index.
  2. Step 2: Identify method for visible text selection

    The method selectByVisibleText(String text) selects the option matching the exact text shown to the user.
  3. Final Answer:

    selectByVisibleText(String text) -> Option D
  4. Quick Check:

    Select by visible text = selectByVisibleText [OK]
Hint: Use selectByVisibleText to pick option by what user sees [OK]
Common Mistakes:
  • Confusing selectByValue with visible text selection
  • Using incorrect method names like selectOptionByText
  • Trying to select by index when text is known
2. Which of the following is the correct syntax to select the third option in a dropdown using Selenium Java's Select class?
easy
A. select.selectByValue(3);
B. select.selectByVisibleText(3);
C. select.selectByIndex(2);
D. select.selectByIndex(3);

Solution

  1. Step 1: Understand index numbering in Select

    Indexing in Select starts at 0, so the third option has index 2.
  2. Step 2: Choose correct method and index

    Using selectByIndex(2) selects the third option correctly.
  3. Final Answer:

    select.selectByIndex(2); -> Option C
  4. Quick Check:

    Index 2 selects third option [OK]
Hint: Remember index starts at 0, so third option is index 2 [OK]
Common Mistakes:
  • Using index 3 for third option (off-by-one error)
  • Passing integer to selectByVisibleText instead of string
  • Using selectByValue with an integer instead of string
3. Given the following dropdown HTML:
<select id="colors">
  <option value="r">Red</option>
  <option value="g">Green</option>
  <option value="b">Blue</option>
</select>

What will be the selected option after executing:
Select select = new Select(driver.findElement(By.id("colors")));
select.selectByValue("g");
medium
A. Green
B. No option selected
C. Blue
D. Red

Solution

  1. Step 1: Identify option with value 'g'

    The option with value attribute "g" is Green.
  2. Step 2: Understand selectByValue behavior

    The method selectByValue("g") selects the option whose value attribute matches "g" exactly.
  3. Final Answer:

    Green -> Option A
  4. Quick Check:

    Select by value 'g' = Green [OK]
Hint: Match the value attribute string exactly to select option [OK]
Common Mistakes:
  • Confusing value attribute with visible text
  • Using selectByVisibleText("g") instead of selectByValue
  • Assuming index 1 selects 'Green' without checking value
4. What is wrong with the following Selenium Java code snippet?
Select select = new Select(driver.findElement(By.id("dropdown")));
select.selectByIndex(5);

Assuming the dropdown has only 4 options indexed 0 to 3.
medium
A. Throws NoSuchElementException because index 5 is out of range
B. Selects the last option automatically
C. No error, selects the 6th option
D. Syntax error due to wrong method usage

Solution

  1. Step 1: Check dropdown option count and index

    The dropdown has 4 options indexed 0 to 3, so index 5 is invalid.
  2. Step 2: Understand selectByIndex behavior on invalid index

    Calling selectByIndex(5) throws NoSuchElementException because the index is out of range.
  3. Final Answer:

    Throws NoSuchElementException because index 5 is out of range -> Option A
  4. Quick Check:

    Invalid index throws exception [OK]
Hint: Check dropdown size before selecting by index to avoid errors [OK]
Common Mistakes:
  • Assuming selectByIndex auto-corrects invalid indexes
  • Thinking it selects last option if index too big
  • Confusing exception type thrown
5. You have a dropdown with options:
<option value="1">Apple</option>
<option value="2">Banana</option>
<option value="3">Cherry</option>
<option value="4">Date</option>

You want to select "Cherry" but only know its position is third in the list. Which code snippet correctly selects "Cherry" using Selenium Java?
hard
A. select.selectByValue("3");
B. All of the above
C. select.selectByVisibleText("Cherry");
D. select.selectByIndex(2);

Solution

  1. Step 1: Understand each selection method

    selectByIndex(2) selects the third option (index starts at 0).
    selectByValue("3") selects option with value "3".
    selectByVisibleText("Cherry") selects option with visible text "Cherry".
  2. Step 2: Confirm all methods select "Cherry"

    All three methods correctly select the "Cherry" option in this dropdown.
  3. Final Answer:

    All of the above -> Option B
  4. Quick Check:

    Index, value, or text all select Cherry [OK]
Hint: Any method works if you know index, value, or visible text [OK]
Common Mistakes:
  • Thinking only one method can select the option
  • Using wrong index (off by one)
  • Confusing value attribute with visible text
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard