Watir-webdriver无法按值识别按钮?

时间:2013-05-15 21:07:49

标签: watir watir-webdriver

据我所知,使用:value作为识别按钮元素的方法应该可以正常工作。甚至有多个示例,例如在watirwebdriver.com网站上显示此内容。但是,当我尝试它时,我得到一个相当标准的无法找到消息

以下是标识按钮的页面中HTML的一部分

<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>

以下是尝试与按钮交互时的情况(在这种情况下通过IRB进行故障排除)

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.. 

任何人都知道为什么我无法通过价值来识别?

2 个答案:

答案 0 :(得分:3)

看起来在使用值构建按钮元素的定位器时有一些特殊的逻辑。

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

使用value时,watir-webdriver似乎正在将其转换为text搜索。我们可以使用value属性和按钮的文字

来验证这一点
b.button(:value => 'An Agent').exists?
#=> true

我唯一的选择是使用css选择器。

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

答案 1 :(得分:0)

试试这行代码:

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

或者,你的选择闪光

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