Cannot identify button element by value?

73 views
Skip to first unread message

Chuck van der Linden

unread,
May 15, 2013, 5:35:12 PM5/15/13
to watir-...@googlegroups.com
See this SO issue for HTML sample, code samples tried in IRB


the short of it.  I've got HTML like

   <button type="button" class="btn fb-user-type" name="fbRadios" value="is_agent" style="">An Agent</button>

 When I try to identify the button by class, or text, it works, but by value  such as 

    b.button(:value => "is_agent").exists?

fails  (that returns false, most other stuff returns the typical cannot be located message)

what the heck?  shouldn't this be working?   (Chrome on a Mac) 

Dan

unread,
May 16, 2013, 9:14:34 AM5/16/13
to watir-...@googlegroups.com
It doesn't look like it's just Chrome actually.  I got the same results in Firefox.  Looks like the stackoverflow answer addresses the issue?  If I change from button type of button to input type of button it works like you would expect.

>> b = Watir::Browser.new :firefox
=> #<Watir::Browser:0x110337500 url="about:blank" title="">
>> b.goto("file:///Users/admin/Desktop/button.html")
=> "file:///Users/admin/Desktop/button.html"
>> b.html
=> "<html xmlns=\"http://www.w3.org/1999/xhtml\"><head></head><body><button value=\"hey\" onclick=\"alert('Hello world!')\" type=\"button\">Click Me!</button></body></html>"
>> b.button.value
=> "hey"
>> b.button(:value => "hey").exists?
=> false
>> b.button(:value => "hey").click
Watir::Exception::UnknownObjectException: unable to locate element, using {:value=>"hey", :tag_name=>"button"}
from /Library/Ruby/Gems/1.8/gems/watir-webdriver-0.6.4/lib/watir-webdriver/elements/element.rb:490:in `assert_exists'
from /Library/Ruby/Gems/1.8/gems/watir-webdriver-0.6.4/lib/watir-webdriver/elements/element.rb:118:in `click'
from (irb):11
# Changed from button to input type of button
>> b.refresh
=> []
>> b.html
=> "<html xmlns=\"http://www.w3.org/1999/xhtml\"><head></head><body><input type=\"button\" value=\"hey\" onclick=\"alert('Hello world!')\" />Click Me!</body></html>"
>> b.button(:value => "hey").exists?
=> true

Chuck van der Linden

unread,
May 17, 2013, 1:34:26 PM5/17/13
to watir-...@googlegroups.com
On Thursday, May 16, 2013 6:14:34 AM UTC-7, Dan wrote:
It doesn't look like it's just Chrome actually.  I got the same results in Firefox.  Looks like the stackoverflow answer addresses the issue?  If I change from button type of button to input type of button it works like you would expect.

>> b = Watir::Browser.new :firefox
=> #<Watir::Browser:0x110337500 url="about:blank" title="">
>> b.goto("file:///Users/admin/Desktop/button.html")
=> "file:///Users/admin/Desktop/button.html"
>> b.html
=> "<html xmlns=\"http://www.w3.org/1999/xhtml\"><head></head><body><button value=\"hey\" onclick=\"alert('Hello world!')\" type=\"button\">Click Me!</button></body></html>"
>> b.button.value
=> "hey"
>> b.button(:value => "hey").exists?
=> false
>> b.button(:value => "hey").click
Watir::Exception::UnknownObjectException: unable to locate element, using {:value=>"hey", :tag_name=>"button"}
from /Library/Ruby/Gems/1.8/gems/watir-webdriver-0.6.4/lib/watir-webdriver/elements/element.rb:490:in `assert_exists'
from /Library/Ruby/Gems/1.8/gems/watir-webdriver-0.6.4/lib/watir-webdriver/elements/element.rb:118:in `click'
from (irb):11
# Changed from button to input type of button
>> b.refresh
=> []
>> b.html
=> "<html xmlns=\"http://www.w3.org/1999/xhtml\"><head></head><body><input type=\"button\" value=\"hey\" onclick=\"alert('Hello world!')\" />Click Me!</body></html>"
>> b.button(:value => "hey").exists?
=> true

 
This is likely because for the input tag of type button, there was no separate text (at least i never see examples of this) the value attribute determined the text that was displayed for the button.. so for that type of element  value=text  The button tag is a lot more robust but seems to be used less often (perhaps due to (misguided?)advice about browser compatibility issues?) 

But the actual button tag, the value and the text are two different things, and you may need to specifically use one or the other to select the element  A great example here might be il8n issues, where it's easier for scripting code to have a consistent value returned by a button, but you may want to display different text based on a language setting (e.g. a bilingual site)  In that case you may also want your automation to identify by the value, as it is predictable.

It seems to me on that basis alone, we ought to be able to predictably select a 'button' tag by its value

Alex Rodionov

unread,
May 18, 2013, 9:27:00 AM5/18/13
to watir-...@googlegroups.com
Reposting myself from SO:

+1 to raise this as issue on watir-webdriver as it definitely needs discussion. I use button(:value) in my tests a lot and it actually matches button's value, so there is something behind this.

According to code, "button" tag name and "value" are treated specifically. According to history, Jari has implemented this when he was making watir-webdriver compliant to watirspec, so I suppose it may come from old Watir implementation. Nevertheless, it needs to be discussed.
Reply all
Reply to author
Forward
0 new messages