16

We are using Selenium WebDriver and JBehave to run "integration" tests on our web-app. I have a method that will enter a value into a form input.

@When("I enter $elementId value $value")
public void enterElementText(final String elementId, final String value) {
    final WebElement webElement = webdriver.findElement(By.id(elementId));
    webElement.clear();
    webElement.sendKeys(value);
}

But when I try to use this to select an item in a drop-down list it (unsurprisingly) fails

java.lang.UnsupportedOperationException: You may only set the value of elements that are input elements

How do I select a value in the combo?

1

4 Answers 4

23

This is how to do it:

@When("I select $elementId value $value")
public void selectComboValue(final String elementId, final String value) {
    final Select selectBox = new Select(web.findElement(By.id(elementId)));
    selectBox.selectByValue(value);
}
10

The Support package in Selenium contains all you need:

using OpenQA.Selenium.Support.UI;

SelectElement select = new SelectElement(driver.findElement( By.id( elementId ) ));
select.SelectByText("Option3");
select.Submit();

You can import it through NuGet as a separate package: http://nuget.org/packages/Selenium.Support

4

By using ext js combobox typeAhead to make the values visible in UI.

var theCombo = new Ext.form.ComboBox({  
...
id: combo_id,
typeAhead: true,
...
});

driver.findElement(By.id("combo_id-inputEl")).clear();
driver.findElement(By.id("combo_id-inputEl")).sendKeys("The Value you need");
driver.findElement(By.id("combo_id-inputEl")).sendKeys(Keys.ARROW_DOWN);
driver.findElement(By.id("combo_id-inputEl")).sendKeys(Keys.ENTER);

If that doesn´t work this is also worth a try

driver.findElement(By.id("combo_id-inputEl")).sendKeys("The Value you need");
driver.findElement(By.className("x-boundlist-item")).click();
1
  • Also works in Java: given some WebElement templateInput, you can do templateInput.sendKeys(STANDARD_TEXT + Keys.ARROW_DOWN + Keys.ENTER);
    – barclay
    Mar 27, 2015 at 20:16
3

The Selenium paradigm is that you are supposed to simulate what a user would do in real life. So that would be either a click or a keys for navigation.

Actions builder = new Actions( driver );
Action  action  = builder.click( driver.findElement( By.id( elementId ) ) ).build();
action.perform();

As long as you get a working selector to feed into findElement you should have no problem with it. I have found CSS selectors to be a better bet for things involving multiple elements. Do you have a sample page?

2
  • Doesn't work for me, I can't find Actions class. BTW using selenium 2.x. But see other SO question which I have marked as duplicate. Aug 3, 2011 at 10:42
  • Download the latest version. I think it used to be called ActionBuilder or something
    – Mike Kwan
    Aug 3, 2011 at 10:50

Your Answer

Reminder: Answers generated by Artificial Intelligence tools are not allowed on Stack Overflow. Learn more

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

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