0
0
Selenium Javatesting~15 mins

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

Choose your learning style9 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