I can’t select HTML Dropdown list with my Webdriver method. What was wrong in my code.? Could you give me some hints.
<select>
<option value="32">32</option>
<option value="34">34</option>
<option value="36">36</option>
</select>
public static List<WebElement> chooseSize(Integer size){
WebElement select = findElement(By.xpath(DropDown_Article_Size_XPATH_ID));
List<WebElement> options = select.findElements(By.tagName("option"));
for(WebElement option : options){
if(option.getText().equals(size)){
option.isSelected(); // or .click()?
}
}
return options;
}
For such cases, I’m using xpath expressions. You’ll save a lot of code!
For what you are asking for, this should do (I assume that your xpath is properly targeting the corresponding
select):By the way, I don’t get why your
chooseSizereturns the list of all options. You should probably rename the method to something meaningful (getOptionsBySize, for example, if this is what you want).