Friday, March 30, 2012

Webdriver - Select list box items

Webdriver - Select list box items

HTML for the list box

I prefer using this code to select an option


The below code also perform the same job

   WebElement ListBox = driver.findElement("ctl00_cpheRFx_ddlCriteriaType"));
   java.util.List<WebElement> options = ListBox.findElements(By.tagName("option"));
   for(WebElement option : options){

Above logic is not working for the below HTML (Below is the possible explanation)

There is one place where the IE driver does not interact with elements using native events. This is in clicking <option> elements within a<select> element. Under normal circumstances, the IE driver calculates where to click based on the position and size of the element, typically as returned by the JavaScript getBoundingClientRect() method. However, for <option> elements, getBoundingClientRect() returns a rectangle with zero position and zero size. The IE driver handles this one scenario by using the click() Automation Atom, which essentially sets the .selected property of the element and simulates the onChange event in JavaScript. However, this means that if the onChange event of the<select> element contains JavaScript code that calls alert(), confirm() or prompt(), calling WebElement's click() method will hang until the modal dialog is manually dismissed. There is no known workaround for this behavior using only WebDriver code.

I am getting empty values for "option.getText()". To over come this Webdriver limitation I have created following Java Script code.

var ListBox = document.getElementById('ctl00_cpheRFx_ddleRFxTitle');
var i;
for (i=0;i<ListBox.length;i++)
if (ListBox.options[i].text == 'S_05_56_52PM29_Mar_2012')
    ListBox.options[i].selected ='1';


javascript:ddleRFxTitle_Change(); //To fire on change event, look at the HTML for better understanding

The same can be implemented in the Webdriver in the following way (I prefer this)

String Script = ""; Script += "var ListBox = document.getElementById('ctl00_cpheRFx_ddleRFxTitle');"; Script += "var i;"; Script += "for (i=0;i<ListBox.length;i++)"; Script += "{"; Script += " if (ListBox.options[i].text == '"+Title+"')"; Script += "{"; Script += "ListBox.options[i].selected ='1';"; Script += "}"; Script += "}"; ((JavascriptExecutor) driver).executeScript(Script); Wait(5000); Script = ""; Script += "javascript:ddleRFxTitle_Change();"; ((JavascriptExecutor) driver).executeScript(Script);



  1. Hi Bharath,

    I want to select the text from list-box.
    Problem is when i select first list-box then the application goes behind all opened applications.
    This problem only occurs when i run my code in IE and it runs fine in Firefox.
    Please check this, why its happening.

    URL link=

    Am using selenium 2.21 and ie8.

    My code-
    driver = new InternetExplorerDriver();
    Select select = new Select(driver.findElement(By.xpath("//form[@id='dataForm']/div[2]/select")));


    1. Hello Mukesh,

      Below code is working fine on IE8.

      String Value = "FUTIDX";

  2. Bharath,

    The code runs but my problem is when it will selecting the value application hides behind all other open aplication in my system.


  3. Bharath

    My application web page has few droplists, when I select first droplist value, page reloads and after that it is throwing StaleElementReferenceException and not able to do anything with driver.

    StaleElementReferenceException: Element not found in the cache - perhaps the page has changed since it was looked up

    I tried different options like using implicit and explicit wait commands, however driver is throwing this exception. Do you have any idea on how to resolve this issue.

    1. Read the following link, similar problem that you are facing