All Ways to Select a Dropdown Value in Selenium
Method Code Snippet (Java) Use Case
1. selectByVisibleText() select.selectByVisibleText("Option 1"); Standard select dropdown with visible label
2. selectByValue() select.selectByValue("option1"); When you know the value attribute
3. selectByIndex() select.selectByIndex(2); Useful when option position is fixed
4. Click + sendKeys dropdown.sendKeys("Option 1"); For simple dropdowns or autocomplete
5. Manual Click (Custom UI) driver.findElement(By.xpath("...")).click(); For non- selectcustom dropdowns
driver.findElement(By.xpath("//li[text()='Option 1']")).click();
6. Actions Class Use Actions to move to dropdown and select For hover-based or dynamic dropdowns
7. JavaScript Executor js.executeScript("arguments[0].value='option1';", dropdown); When traditional methods fail (hidden/styled elements)
8. Robot Class Use Robot to simulate key presses like Arrow Down + Enter Legacy or desktop-style dropdowns
9. Select using CSS/JS via JS executeScript("document.querySelector('#dropdown').value='option1'") Advanced workaround for React/Angular-based UIs
🔹 Example for Custom Dropdown:
// Click dropdown to open options
driver.findElement(By.id("dropdownBtn")).click();
// Click desired option
driver.findElement(By.xpath("//li[text()='Option 2']")).click();
✅ Summary:
Use Select class for standard HTML select elements.
Use manual click + XPath/CSS for custom dropdowns (React, Angular, etc.).
Use JavaScript or Actions when elements are hidden or complex.