A couple of issues were seen when locating an element based on its label
(and/or retrieving the element's label text):
1. Locating the element's label is based on the element's name rather than id.
2. It is only possible to locate text fields based on their label (ie other
input fields cannot be located by their label).
3. All elements with no name/id attribute, such as when the element is a child
of its label, are associated with the first label on the page that has no for
attribute.
4. It is being assumed that the element and label belong to the same container,
which might not be true when chaining elements.
### 1. Locating the element's label is based on the element's name rather than
id.
Given the html:
````html
<label for="new_user_first_name" id="first_label">First name</label>
<input name="new_user_first_name" id="new_user_first_name" class="name" />
<label for="new_user_last_name" id="last_label">Last name</label>
<input name="something_else" id="new_user_last_name" class="name" />
````
The correct result when the input name that matches the id:
````ruby
p browser.text_field(:label => 'First name').exists?
#=> true
````
An unexpected result when the input name does not match the id:
````ruby
p browser.text_field(:label => 'Last name').exists?
#=> false
````
### 2. It is only possible to locate text fields based on their label (ie other
input fields cannot be located by their label).
Given the html:
````html
<input type="checkbox" name="new_user_interests" id="new_user_interests_books"
value="books" checked="checked" />
<label for="new_user_interests_books">Books</label>
````
It is not possible to find the checkbox by its label (as it appears to be
comparing a Watir::Label object):
````ruby
p browser.checkbox(:label, 'Books').exists?
#=> false
````
### 3. All elements with no name/id attribute, such as when the element is a
child of its label, are associated with the first label on the page that has no
for attribute.
Given the html:
````html
<label>Found <input type="text"></label>
<label>Lost <input type="text"></label>
````
The second text field cannot be found by its label text, "Lost", as it appears
to be associated with the first label's text, "Found".
````ruby
p browser.text_field(:label => /Lost/).exists?
#=> false
p browser.text_field(:index => 1).label
#=> "Found"
````
### 4. It is being assumed that the element and label belong to the same
container, which might not be true when chaining elements.
Given the html:
````html
<div>
<span class="label">
<label for="my_label">My Label</label>
</span>
<span class="field">
<input type="text" name="my_label" id="my_label"/>
</span>
</div>
````
You can find the text field by its label when the container of the text field
includes the label:
````ruby
p browser.text_field(:label => 'My Label').exists?
#=> true
p browser.text_field(:name => 'my_label').label
#=> "My Label"
````
However, if you reduce the container to only include the text field, the label
is no longer found:
````ruby
p browser.span(:class => 'field').text_field(:label => 'My Label').exists?
#=> false
p browser.span(:class => 'field').text_field(:name => 'my_label').label
#=> `assert_exists': Unable to locate element, using {:tag_name=>["label"],
:for=>"my_label"} (Watir::Exception::UnknownObjectException)
````
---
Reply to this email directly or view it on GitHub:
https://github.com/watir/watir-classic/issues/56
_______________________________________________
Wtr-development mailing list
[email protected]
http://rubyforge.org/mailman/listinfo/wtr-development