Thursday, October 20, 2011

Selecting Multiple Items in Listbox

Selecting multiple HTML Listbox's options

String strURL;
strURL="URL";

WebDriver firefoxDriver=new FirefoxDriver();
firefoxDriver.get(strURL);

WebElement list1;
list1=firefoxDriver.findElement(By.name("lst"));

List<WebElement> lstOptions=list1.findElements(By.tagName("option"));
list1.sendKeys(Keys.CONTROL);
lstOptions.get(0).click();//Selects the first option.
lstOptions.get(1).click();//Selects the second option.

3 comments:

  1. I got the exact one after long search..
    Thanks a lot :)

    ReplyDelete
  2. WebElement list = driver.findElement(By.id("sel"));
    List listOpts = list.findElements(By.tagName("option"));

    int lastIndex = listOpts.size()-1;
    Actions act = new Actions(driver);
    act.clickAndHold(listOpts.get(0)).perform();
    act.moveToElement(listOpts.get(lastIndex)).release().perform();

    This worked for me.

    ReplyDelete
  3. Thanks for the help... It worked

    ReplyDelete