Web Elements
Watir automatically generates classes and methods for all HTML5.1 and SVG2 supported elements and their associated attributes. These classes leverage encapsulation to provide access to their specific attributes, making them easier to locate and use.
You can see an example of filling out a form in our Example Code
Overview
Watir::Element is the superclass
that all other Element types inherit from. All Elements have these behaviors.
It is accessed by calling Browser#element.
Creating an ElementCollection is accomplished with Browser#elements.
For all of the different ways of locating an Element,
take a look at our Locating Elements Guide.
You are discouraged from using this class directly, and use the correct subclass for the element type you are using.
Common Methods
Standard Setter Interface
This functionality was fully implemented in Watir 7 beta 2.
One of the coolest features implemented by Page Object gems is the ability to use Watir to automatically
fill out a form based on Element type. As of Watir 7.0, every element will respond to #set and take
an argument that ensures that the correct action will be taken for that element. Many of the element
subclasses in the following section will take a specific obvious action
(follow the links below to learn what they are).
If the subclass isn’t specified, though, Watir will use Element#set to determine what kind of element
it is and take the desired action based on that.
Example
browser = Watir::Browser.start 'watir.com/examples/simple_form.html'
header = browser.element(id: 'user')
header.present? # => true
header.text == 'Add user' # => true
header.attribute('data-test') == 'header' # => true
header.click
browser.closeSpecial Handling of Specific Elements
- Buttons (Button)
- CheckBoxes (CheckBox)
- Dropdowns (Select & Option)
- Frames (Frame & IFrame)
- Links (Anchor)
- Radio Buttons (Radio & RadioSet)
- Text Inputs (TextField & TextArea & WYSIWYG)