Chuck van der Linden
Chuck van der Linden

Reputation: 6660

Watir-webdriver unable to identify button by value?

As far as I know, using :value as a way to identify a button element should work just fine. There are even multiple examples such as on the watirwebdriver.com site that show this. However when I attempt it, I get a fairly standard unable to locate message

Here's a portion of the HTML from the page where the button is identified

<div class="data-form-holder row-fluid">
    <h4>I am:</h4>
    <div class="btn-group" data-toggle="buttons-radio">
      <button type="button" class="btn fb-user-type" name="fbRadios" value="is_agent">An Agent</button>
      <button type="button" class="btn fb-user-type" name="fbRadios" value="is_grower">A Grower</button>
    </div>
  </div>

And here is what happens when trying to interact with the button (via IRB in this case, to troubleshoot)

1.9.3p125 :006 > b.buttons(:class => "fb-user-type").count
 => 2   # yep, I can find the buttons I want, so not a frame issue etc.
1.9.3p125 :008 > b.button(:class => "fb-user-type").value
 => "is_agent"   # I can even get the value of the first one, which is as expected./
1.9.3p125 :009 > b.button(:value => "is_agent").flash
Watir::Exception::UnknownObjectException: unable to locate element, using {:value=>"is_agent", :tag_name=>"button"}
    from /Users/cvanderlinden/.rvm/gems/ruby-1.9.3-p125/gems/watir-webdriver-0.6.4/lib/watir-webdriver/elements/element.rb:490:in `assert_exists'
    from /Users/cvanderlinden/.rvm/gems/ruby-1.9.3-p125/gems/watir-webdriver-0.6.4/lib/watir-webdriver/elements/element.rb:420:in `style'
    from /Users/cvanderlinden/.rvm/gems/ruby-1.9.3-p125/gems/watir-webdriver-0.6.4/lib/watir-webdriver/elements/element.rb:232:in `flash'
    from (irb):9
    from /Users/cvanderlinden/.rvm/rubies/ruby-1.9.3-p125/bin/irb:16:in `<main>'
#well that was annoying, I used a value I know exists, it just read it to me, but 
# I cannot find the button that way?

1.9.3p125 :004 > b.buttons(:value => "is_agent").count
 => 0   # kinda expected since flash on a single button failed.. 
1.9.3p125 :018 > b.button(:text => "An Agent").flash  #this works
 => #<Watir::Button:0x7d6f5d1340a3b6b8 located=true selector={:text=>"An Agent", :tag_name=>"button"}> 
1.9.3p125 :020 > b.button(:text => "An Agent").value
 => "is_agent"   #also shows me the value I expect
1.9.3p125 :022 > b.button(:text => "An Agent").attribute_value("value")
 => "is_agent"   #same answer via another means
1.9.3p125 :023 > b.button(:value => "is_agent").exists?
 => false        #and yet, finding it by value, supported but failing.. 

Anyone with an idea why I can't identify by value?

Upvotes: 3

Views: 2628

Answers (2)

Gaurav Sharma
Gaurav Sharma

Reputation: 349

Try this Line of code:

browser.div(:class, "btn-group").button(:value, "is_agent").exist?

OR, Your choice flash

browser.div(:class, "btn-group").button(:value, "is_agent").flash

Upvotes: 0

Justin Ko
Justin Ko

Reputation: 46846

It looks like there is some special logic in the building the locator for button elements using value.

In button_locator.rb, line 41:

def lhs_for(key)
  if @building == :input && key == :text
    "@value"
  elsif @building == :button && key == :value
    "text()"
  else
    super
  end
end

It appears that when using value, watir-webdriver is converting it to a text search. We can verify this by using the value attribute and the button's text:

b.button(:value => 'An Agent').exists?
#=> true

The only alternative I have is to use a css selector.

b.element(:css => 'button[value="is_agent"]').exists?
#=> true

Upvotes: 3

Related Questions