views:

1634

answers:

1

Hi,

I'm trying to select a value in a select element. I'm using Selenium RC (Java) to run the test cases. I understand that the code for selecting a value is given by:

selenium.select("locator", "value=REQUIRED VALUE")

I'm unable to select the desired value with the above code. I think it might be something to do with optgroup in the select source code. I do not get any exceptions, the command executes fine but looking at the page the required value is not selected. Also, I cant use ids (instead of value) because there arent any. Here is the source code of the selector:

<select>
   <optgroup label="Group1">
      <option value="13">some value1</option>
      <option value="25">some value2</option>
   </optgroup>
   <optgroup label="Group2">
      <option value="18">REQUIRED VALUE</option>
      <option value="34">some value3</option>
      ...
      ...
   </optgroup>
</select>

Is there any way to select the required value using Selenium?

It would be great if we could avoid the option values (such as "18", "34" etc) because these numbers change later as the values change. For example, "REQUIRED VALUE" has a value -18 but if I were to delete this item and add it again its value would be different. Basically this drop-down box is dynamic.

+2  A: 

The value for the required option in your example is actually '18'. Try the following:

selenium.select("locator", "label=REQUIRED VALUE")
Dave Hunt
Hi Dave,Thanks for replying. But do you think there is another way besides using 18? The contents of the drop down change in my application and so do the numbers. In other words, right now its showing as but it can be any number. Even if I run my tests multiple times these numbers change. Do you have any other ideas as to what can be done? Meanwhile I'll update this comment on my question too.Thanks,Mugen
Mugen
Check my answer again. I am suggesting changing 'value=' to 'label='. This allows you to select an option based on its label instead of its value.
Dave Hunt
Thanks for answering. Its working perfectly with label!
Mugen