Day 25: Dropdown Handling in Selenium - Select Class & Dynamic Dropdowns
Master dropdown handling techniques in Selenium: Select class for HTML select tags and iteration methods for dynamic dropdowns
Today's Learning Objectives
Dropdown Fundamentals
- • Understanding dropdown types in web applications
- • HTML select tag vs custom dropdowns
- • Select class methods and usage
- • StaleElementReferenceException handling
Advanced Techniques
- • Dynamic dropdown iteration methods
- • Actions class for dropdown handling
- • JavaScriptExecutor for complex scenarios
- • Real-world application examples
Types of Dropdowns in Web Applications
1. HTML Select Tag Dropdowns
Identification & Handling
First, check the tagname of dropdown. If dropdown tagname starts with <select> tag, then we handle dropdown using Select class from Selenium.
HTML Structure Example
<option value="in">India</option>
<option value="us">USA</option>
<option value="uk">UK</option>
</select>
Select Class Methods
selectByVisibleText()
Select by the visible text displayed to user
selectByIndex()
Select by index position (starts from 0)
selectByValue()
Select by the value attribute
2. Custom/Dynamic Dropdowns
Non-Select Tag Dropdowns
If dropdown tagname starts without select tag (div, span, ul, li, a, p, h1-h6, etc.), we use different approaches.
Three Handling Methods
- 1. Using Actions class
- 2. Using JavaScriptExecutor interface
- 3. By iterating all dropdown values (Most Common)
Method 3: Iterating Dropdown Values
Step-by-Step Process
Step 1: Click on Dropdown Element
Step 2: Find All Dropdown Values
driver.findElements(By.xpath("//div[@class='option']"));
Step 3: Iterate Using Enhanced For Loop
// Step 4: Capture dropdown values
String actualValue = value.getText();
// Step 5: Check if expected value matches
if(actualValue.equals("Haryana")) {
// Step 6: Click on the value
value.click();
// Step 7: Break the loop
break;
}
}
⚠️ StaleElementReferenceException
What is StaleElementReferenceException?
This exception occurs when the previous element existed in HTML DOM structure but currently the element is not present in HTML DOM structure.
Common Cause
If you don't break the loop after finding and clicking the expected value, the loop continues and tries to access elements that are no longer available in DOM.
Real-World Examples
Example 1: DemoQA Practice Form
Website: https://demoqa.com/automation-practice-form
Task: Select Haryana from State Dropdown
Complete Java Code
driver.manage().window().maximize();
driver.get("https://demoqa.com/automation-practice-form");
// Click on state dropdown
driver.findElement(By.xpath("//div[@id='state']/descendant::div[@class=' css-1hwfws3']")).click();
// Find all dropdown values
List<WebElement> listValues = driver.findElements(
By.xpath("//div[@class=' css-11unzgr']/child::div"));
// Iterate and select Uttar Pradesh
for(WebElement value : listValues) {
String actualValue = value.getText();
if(actualValue.equals("Uttar Pradesh")) {
value.click();
break;
}
}
Example 2: OrangeHRM Employee Management
Website: https://opensource-demo.orangehrmlive.com/
Complete Employee Creation Workflow
Key Dropdown Selections
Nationality Dropdown:
Select "Indian" from nationality options
Marital Status:
Select "Other" from marital status options
User Role:
Select "ESS" from user role dropdown
Advanced Techniques Used
- • Employee ID capture using getAttribute("value")
- • Actions class for employee name autocomplete
- • Keyboard navigation with Keys.ARROW_DOWN and Keys.ENTER
- • Complex XPath with parent/child/sibling navigation
Example 3: Cogmento CRM
Website: https://ui.cogmento.com/
Contact Creation Workflow
Multiple Dropdown Selections
- • Access: Select dropdown option
- • Status: Select "On Hold" value
- • Category: Select "Customer Category"
- • Social Channels: Select "TikTok"
- • Country: Select "India" value
- • Source: Select "Google" source
- • Birthday: Date picker selection
Advanced Dropdown Techniques
Actions Class Method
Employee Name Autocomplete
act.sendKeys(employeeName, "Rohini Patil")
.build().perform();
act.keyDown(Keys.ARROW_DOWN).perform();
act.keyDown(Keys.ENTER).build().perform();
Benefits
- • Handles autocomplete dropdowns
- • Keyboard navigation support
- • More realistic user interaction
Best Practices
✅ Do's
- • Always break the loop after selection
- • Use explicit waits for dropdown loading
- • Verify dropdown is clickable before interaction
- • Handle StaleElementReferenceException
❌ Don'ts
- • Don't forget to break the loop
- • Avoid hardcoded Thread.sleep()
- • Don't assume dropdown structure
- • Avoid selecting by index for dynamic content
Knowledge Check
When should you use the Select class in Selenium for dropdown handling?
Key Points Summary
Dropdown Handling
- • Use Select class for <select> tag dropdowns
- • Use iteration method for custom dropdowns
- • Always break the loop after successful selection
- • Handle StaleElementReferenceException properly
Advanced Techniques
- • Actions class for autocomplete dropdowns
- • Keyboard navigation for better user simulation
- • Explicit waits for dynamic content loading
- • Complex XPath for nested dropdown structures