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 Methods Supported by Element.
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
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