Testing / Selenium Interview questions
How do you handle dropdowns in Selenium using the Select class?
For a native HTML <select> element, Selenium's Select class wraps the raw WebElement with dropdown-specific methods, rather than requiring manual clicks on each option.
Select dropdown = new Select(driver.findElement(By.id("country"))); dropdown.selectByVisibleText("Canada"); dropdown.selectByValue("CA"); dropdown.selectByIndex(2);
These three selection methods cover the common ways an option might need to be identified - by what the user sees, by the underlying value attribute, or by position. Select only works for genuine <select> elements, though; a custom-styled dropdown built from <div> elements and JavaScript has no native select semantics, so it must be handled with plain clicks on the visible option elements instead.
The class also exposes read methods like getFirstSelectedOption() and isMultiple(), useful for verifying current selection state or working with a multi-select list where several options can be chosen at once.
More Related questions...