Reputation: 105
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;
}
Upvotes: 2
Views: 12229
Reputation: 11
Bit modification it works for me, thanks a lot such a simple code it does the job.
Select select = new Select(driver.findElement(By.name("Status_operator")));
select.selectByValue("=");
Upvotes: 1
Reputation: 350
you can do
WebElement selectElement = driver.findElement(By.xpath(DropDown_Article_Size_XPATH_ID));
selectElement.sendKeys("34")
to select 34
its that simple. Sendkeys is a very useful method in webdriver and has different implementations for different kind of objects i.e. for a textbox Sendkeys would type in the text, while for a select element it would select element.
I have even read that for a file upload field you can do sendkeys to enter the file path.
cheers
Shrikant
Upvotes: 0
Reputation:
Select select = new Select(driver.findElement(By.xpath("Xpath_of_Select_Element")));
select.selectByVisibleText("Option_to_Select");
This is the simplest way to select an option from a select drop down
Upvotes: 2
Reputation: 155
I'm afraid that there's an issue with ChromeDriver and Select. Tested on Chrome for MacOSX, .click() and .isSelected() don't work. The same code in FireFox, works as expected. Is there anything different between both browsers?
List<WebElement> opciones = select.getOptions();
for(WebElement el : opciones){
System.out.println("Elemento disponible: ["+el.getAttribute("value")+"]["+el.getText()+"]");
//Select actual option
el.click();
if(el.isSelected())
System.out.println("Selected: ["+el.getAttribute("value")+"]["+el.getText()+"]");
}
Upvotes: 0
Reputation: 1021
There's a support class that can help you with that in WebDriver: "org.openqa.selenium.support.ui.Select".
Here is how you use it:
// First, get the WebElement for the select tag
WebElement selectElement = driver.findElement(By.xpath(DropDown_Article_Size_XPATH_ID));
// Then instantiate the Select class with that WebElement
Select select = new Select(selectElement);
// Get a list of the options
List<WebElement> options = select.getOptions();
// For each option in the list, verify if it's the one you want and then click it
for (WebElement we : options) {
if (we.getText().equals(valueToSelect)) {
we.click();
break;
}
}
Upvotes: 3
Reputation: 5141
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
):
// Click select first:
// (See http://code.google.com/p/selenium/issues/detail?id=2112)
findElement(By.xpath(DropDown_Article_Size_XPATH_ID)).click();
// Then click option:
String xpathOption = String.format("%s/option[text()='%d']",
DropDown_Article_Size_ID, size);
log.debug("Selecting option by text '{}' using xpath '{}'", size, xpathOption);
findElement(By.xpath(xpathOption)).click();
By the way, I don't get why your chooseSize
returns the list of all options. You should probably rename the method to something meaningful (getOptionsBySize
, for example, if this is what you want).
Upvotes: 1
Reputation: 32407
Have you tried setSelected()
? isSelected()
is a getter so it won't change anything.
Upvotes: 0