Dashboard > Selenium > ... > Contributed User-Extensions > locateByLabelText
Selenium Log In View a printable version of the current page.
locateByLabelText
Added by Matt Wizeman, last edited by Matt Wizeman on Jan 05, 2006  (view change)
Labels: 
(None)

description

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.

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

notes

  • Use this extension with Selenium 0.6.0.
  • 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 and Firefox 1.0.7

download

findByLabelText_1_0

Thanks for your extention. I installed it and got it to work.
Before that, I used this code pasted in as a "dom=" selector.

function(labelText) {
    var labels = inDocument.getElementsByTagName("label");
    var inDocument=selenium.browserbot.getCurrentWindow().document;
    for (var i = 0; labels && i < labels.length; i++) {
         var currentLabel=labels[i];
         if (currentLabel.innerHTML.match(labelText)) {
           var forAttribute = currentLabel.attributes['for'] ? currentLabel.attributes['for'].value : null;
           return inDocument.getElementById(labels[i].getAttribute("for"));
         }
    }
    throw new SeleniumError("Unable to get label with text '" + labelText + "'");
};
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