Selection Boxes
Watir sets or clears an item in a selection box (or dropdown box) by looking at the attributes available in the <select> HTML tag. Common attributes are id and name. For complete list see HTML Elements Supported by Watir.
What you see in the web browser:
This is the tag in the HTML source:
<select id="one" name="selectme"> <option></option> <option>Web Testing</option> <option>in Ruby</option> <option>is fun</option> </select>
id Attribute
Watir code to set a select box item using the id attribute:
ie.select_list(:id, "one").set("is fun")
name Attribute
Watir code to set a select box item using the name attribute:
ie.select_list(:name, "selectme").set("is fun")
Selection Box Methods
Watir code to clear a select box item using the id attribute:
ie.select_list(:id, "one").clearSelection
Watir code to get the contents of a select list:
contents = ie.select_list(:id, "one").getAllContents
NOTE: contents will be an array. This no longer seems to work.
Select Multiple
Some select lists can have multiple selections instead of just one. If multiple items can be selected in select box, Watir can set or clear an item.
What you see in the web browser:
This is the tag in the HTML source:
<select id="one" name="selectme" multiple="multiple"> <option></option> <option>Web Testing</option> <option>in Ruby</option> <option>is fun</option> </select>
You can set individual options using successive _set_s and you can clear everything that is selected with the clearSelection method. The following code would select every option in the select list and then clear everything.
ie.select_list(:id, 'one').set('Web Testing')
ie.select_list(:id, 'one').set('in Ruby')
ie.select_list(:id, 'one').set('is fun')
ie.select_list(:id, 'one').clearSelection
Comments (9)
Aug 27, 2008
Heba Hosny says:
in the id_attribute section, the command should be ie.select_list(bla blain the id_attribute section, the command should be ie.select_list(bla bla
Feb 06, 2009
Bill Mosteller says:
I tried to display the setting with ie.select_list(bla bla).selected_options and...I tried to display the setting with ie.select_list(bla bla).selected_options and Ruby said, "undefined method `selected_options' for #<Watir::SelectList:x3f5fc64> (NoMethodError)"
How do I display how the control is set? I tried .text but it seems to give me everything.
Thanks.
Bill Mosteller
Eloqua
Jun 14, 2009
rohan ojha says:
Hi Bill I assume your query is how to get the value of the current option selec...Hi Bill
I assume your query is how to get the value of the current option selected in the select list.
Well the below code might help you in getting the current selcted option.However the method checks for each and every option in the select list and only then returns the currently selected value.So it might be a bit time consuming.
def sel_list_opt(sel_list)
arr=sel_list.getAllContents.to_a
d=arr.length.to_i
i=0
while 0 .. d
if sel_list.selected?(arr[i].to_s) == true
return arr[i].to_s
else
i=i+1
end
end
end
IF anybody has a better way of handling this please let me know as well.But this should do the work alright.
Cheers
Rohan Ojha
Jun 22, 2009
rohan ojha says:
Well dont know how I missed it. But there is an inbuilt method that gets you th...Well dont know how I missed it.
But there is an inbuilt method that gets you the value of the current selected item in the list.
Its "getSelectedItems" and it returns the current selected value without iterating through the entire options in the combo box.
Cheers
Rohan Ojha
Feb 22, 2010
Ben DeGroot says:
I was wondering if it's possible to select an item in the list by index. L...I was wondering if it's possible to select an item in the list by index. Let's say I don't actually care what the value in the list is or the application might be dynamic and I may be not certain of the value. However, that field may be required on the screen and all I care about is that it has a value so I can move on to the next page.
I just want to select by index. For instance I was expecting to see code something like this: ie.select_list(:id, 'one').setByIndex(0)
Does anyone have any suggestions?
Apr 01, 2010
Chandrashekar Babu says:
A very un-elegant way of doing it would be something as below: &nb...A very un-elegant way of doing it would be something as below:
ie.select_list(:id, "one").set(ie.select_list(:id, "one").options[1])
There must definitely be a better way of setting options by indexes.
Apr 08, 2010
Harry says:
There is the select_item_in_select_list method to select an option by ...There is the select_item_in_select_list method to select an option by an attribute.
Unfortunately it does not work with :index (since select options only provide a :text or a :value), but at least you could try a numeric :value.
(In our application we use value aliases starting with 1 for security and match them on the server side to the actual entity IDs. In this case the method is pretty useful.)
Aug 31, 2011
frank xia says:
I think there is a wrong decription about the method of selection. The element ...I think there is a wrong decription about the method of selection.
The element of select always supports just one selection,and I have tried browser.select_list(:id,"xxx").select("option_name"). what you see! It has done!!
Mar 22, 2012
Rahul says:
Thanks Frank. browser.select_list(:id,"xxx").select("option_name") is right.Thanks Frank.
browser.select_list(:id,"xxx").select("option_name") is right.