2

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;
}
4
  • Which browser is webdriver driving?
    – skaffman
    Jul 25, 2011 at 8:56
  • right now I tested on Firefox
    – KoKo
    Jul 25, 2011 at 8:58
  • I still have problem with it.
    – KoKo
    Jul 25, 2011 at 11:10
  • what about select by index? have you tried?
    – user1102510
    Dec 16, 2011 at 18:14

8 Answers 8

3

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;
    }
}
1
  • With the current last WebDriver version (2.12) the org.openqa.selenium.support.ui.Select class provides convenients methods : selectByIndex(int), selectByValue(String), selectByVisibleText(String) to simulate selection. I tested it successfully with Firefox 8. Dec 9, 2011 at 16:03
2
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

1

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).

1

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("=");
0

Have you tried setSelected()? isSelected() is a getter so it won't change anything.

0

If you are using Selenium2 you have to use option.click().

0

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()+"]");
}
0

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.