Select class for dropdowns in Selenium Java - Build an Automation Script
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 org.testng.Assert; import org.testng.annotations.AfterClass; import org.testng.annotations.BeforeClass; import org.testng.annotations.Test; import java.time.Duration; public class DropdownSelectTest { WebDriver driver; WebDriverWait wait; @BeforeClass public void setUp() { // Set path to chromedriver if needed driver = new ChromeDriver(); wait = new WebDriverWait(driver, Duration.ofSeconds(10)); driver.get("https://example.com/dropdownpage"); } @Test public void testSelectOptionByVisibleText() { // Wait until dropdown is visible WebElement dropdownElement = wait.until( ExpectedConditions.visibilityOfElementLocated(By.id("dropdownMenu")) ); // Create Select object Select dropdown = new Select(dropdownElement); // Select option with visible text 'Option 2' dropdown.selectByVisibleText("Option 2"); // Verify selected option is 'Option 2' String selectedOption = dropdown.getFirstSelectedOption().getText(); Assert.assertEquals(selectedOption, "Option 2", "Selected option should be 'Option 2'"); } @AfterClass public void tearDown() { if (driver != null) { driver.quit(); } } }
This test script uses Selenium WebDriver with Java and TestNG framework.
In setUp(), we open the browser and navigate to the page with the dropdown.
In the test method, we wait explicitly for the dropdown to be visible using WebDriverWait and ExpectedConditions. This avoids timing issues.
We locate the dropdown by its id and create a Select object to interact with it.
We select the option by its visible text 'Option 2' using selectByVisibleText.
Then we get the currently selected option's text and assert it equals 'Option 2' to confirm the selection worked.
Finally, in tearDown(), we close the browser to clean up.
This structure follows best practices: explicit waits, meaningful locators, using the Select class, and proper assertions.
Now add data-driven testing to select and verify three different options: 'Option 1', 'Option 2', and 'Option 3'.
Practice
Select class in Selenium WebDriver?Solution
Step 1: Understand the Select class role
The Select class is designed specifically to interact with dropdown menus that use the <select> HTML element.Step 2: Compare with other Selenium features
Locating elements, mouse actions, and screenshots are handled by other classes and methods, not Select.Final Answer:
To handle dropdown menus created with the <select> HTML tag -> Option CQuick Check:
Select class = dropdown handler [OK]
- Using Select for non-<select> dropdowns
- Confusing Select with Actions class
- Thinking Select locates elements
dropdown?Solution
Step 1: Recall Select class constructor syntax
The Select class constructor requires the WebElement as an argument and is called with 'new Select(element)'.Step 2: Check Java syntax correctness
Java is case-sensitive; class names start with uppercase. The correct syntax is 'new Select(dropdown);'. Other options have syntax errors or wrong class names.Final Answer:
Select select = new Select(dropdown); -> Option BQuick Check:
Use 'new Select(element)' to create Select object [OK]
- Missing 'new' keyword
- Wrong class name like SelectElement
- Incorrect capitalization
WebElement dropdown = driver.findElement(By.id("colors"));
Select select = new Select(dropdown);
select.selectByIndex(2);
String selected = select.getFirstSelectedOption().getText();Assuming the dropdown options are:
0: Red
1: Green
2: Blue
3: Yellow
Solution
Step 1: Understand selectByIndex behavior
selectByIndex(2) selects the option at index 2, counting from 0.Step 2: Map index to option text
Index 0 = Red, 1 = Green, 2 = Blue, so the selected option is "Blue".Final Answer:
"Blue" -> Option AQuick Check:
Index 2 = Blue [OK]
- Assuming index starts at 1
- Confusing selectByIndex with selectByValue
- Misreading option order
WebElement dropdown = driver.findElement(By.name("fruits"));
Select select = new Select(dropdown);
select.selectByVisibleText("Apple");Assume the dropdown is not a
<select> element but a custom div-based dropdown.Solution
Step 1: Check element type requirement for Select
Select class works only with <select> HTML elements. Using it on a non-<select> element causes an error.Step 2: Identify exception thrown
When Select is used on a non-<select> element, Selenium throws UnexpectedTagNameException.Final Answer:
selectByVisibleText throws UnexpectedTagNameException -> Option DQuick Check:
Select requires <select> tag, else UnexpectedTagNameException [OK]
- Assuming it throws NoSuchElementException
- Ignoring element tag type
- Expecting NullPointerException
"Small", "Medium", "Large", "Extra Large"
Write the best way to select "Extra Large" using the Select class if you only know the option's value attribute is "xl" but the visible text is dynamic.
Which method should you use?
Solution
Step 1: Understand selection methods
selectByVisibleText requires exact visible text, which is dynamic here, so not reliable.Step 2: Use value attribute for stable selection
selectByValue("xl") uses the option's value attribute, which is stable and known.Step 3: Evaluate other options
selectByIndex(3) depends on position which may change; selectByPartialVisibleText does not exist in Select class.Final Answer:
select.selectByValue("xl"); -> Option AQuick Check:
Use selectByValue for stable attribute selection [OK]
- Using selectByVisibleText with dynamic text
- Using invalid method selectByPartialVisibleText
- Relying on index which may change
Ready to prove it?
Pass the test and this pattern moves unknown
