Dashboard > Watir > ... > Simple > Selection Boxes
Watir Log In View a printable version of the current page.
Selection Boxes
Added by Zeljko, last edited by Alan Baird on Jun 21, 2008  (view change)
Labels: 

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
Go to contents, previous, next page.

Site running on a free Atlassian Confluence Community License granted to OpenQA. Evaluate Confluence today.
Powered by Atlassian Confluence, the Enterprise Wiki. (Version: 2.5.6 Build:#812 Aug 06, 2007) - Bug/feature request - Contact Administrators