3

据我所知,使用 :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.. 

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

4

2 回答 2

3

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

button_locator.rb 中,第 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
于 2013-05-16T01:14:11.327 回答
0

试试这行代码:

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

或者,您的选择闪光

browser.div(:class, "btn-group").button(:value, "is_agent").flash
于 2013-05-20T10:52:58.243 回答