Sunday, November 27, 2011

Webdriver - Drop down select values

Webdriver - Drop down select values. (IE Driver)

There is no direct method to choose/click the values from the drop down.
I have used following code to select the list values. Also attaching the screen shot of the list box HTML for better understanding.


String UOMCode = "A1";
driver.findElement(By.xpath("//select[@id='ddlUOM']/option[@value='"+UOMCode+"']")).click();

Also read following post
http://bharath-marrivada.blogspot.in/2012/03/webdriverselenium2-listboxselectdropdow.html

----

5 comments:

  1. well you need to create object for "Select" and use it to select value from drop down.

    ReplyDelete
  2. Not required. Above code works perfectly fine to me.

    ReplyDelete
  3. This examples is too complex and require to write tricky xpath. Better to use

    Select options = Select(driver.findElement(By.id('ddlUOM'));
    options.selectByValue('UOMCode');

    than better to move WebElement into the PageObject to make code more reusable.

    ReplyDelete
  4. Hello Andrew,

    Your code will not working for my application, so I have taken different approach.

    ReplyDelete
  5. please share your approach as above one is not working for my application.

    ReplyDelete