WebDriver Tips: SELECTs

One of the hidden gems of webdriver is the support library. This contains useful classes and utilities for dealing with common situations and cases. We recently checked in a wrapper for SELECT elements. Beforehand, interacting with a SELECT could be a little verbose:

WebElement select = driver.findElement(By.tagName("select"));
List<WebElement> options = select.findElements(By.tagName("option);
for (WebElement option : options) {
  if ("want this".equals(option.getValue()) {
    option.setSelected();
    break;
  }
}

With the Select support class, this becomes:

Select element = driver.findElement(By.tagName("select"));
Select select = new Select(element);
select.selectByValue("want this");

This does the same job, but someone reading your test can now gather the intent that little bit more easily.

The Select class will be made more widely available when do our next release, hopefully near the end of this month.


Simon Stewart on Thursday, 19 February, 2009

Posted in: /tech/webdriver

You may comment...



Added by Rob on Wednesday, 27 May, 2009

The snippet is possibly incorrect; Did you mean:

WebElement element = driver.findElement(By.tagName("select")); Select select = new Select(element); select.selectByValue("want this");

Categories