Locate form elements in selenium tests by matching the text in their associated labels. A label can be associated with a form control by either setting its "for" attribute to the id of the form control or by nesting the form control within the label tag. This locator supports both methods and will support nested input, select, textarea and button tags. It supports some filters (notably index) and also a pseudofilter: within, which restricts the search for matching labels to a subtree of the infoset whose tree is identified by a given xpath expression.
example of use
For a form containing the HTML:
<label for="inputWithId">Input with ID:</label> <input id="inputWithId" /> <label>Nested Input: <input id="nestedInput" /></label>
Text can be typed into the two inputs using the following selenium commands:
|type||labelText=Input with ID:||input found by id|
|type||labelText=Nested Input*||nested input found|
- Use this extension with Selenium 0.6.0 or better. Success has been reported with Selenium 1.0b2.
- The text can be specified using a pattern.
- This extension will throw an exception under a few circumstances:
- If a label with matching text is not found.
- If a label with matching text is found but it has no associated form control.
- If a label with matching text is found and its for attribute points to an ID that doesn't exist.
- If a label with matching text is found and it has more than one nested form control.
- This extension has been tested in IE 6.0, Firefox 1.0.7, Firefox 3.1b3, Safari 4 build 528.16, Opera 9.64 build 10487, and Google Chrome 184.108.40.206.
This user extension is available under Apache License 2.0.