如何找到Capybara中的第一个复选框?

bte*_*les 8 cucumber capybara

我想找到使用capybara dsl的复选框的第一个实例.谁知道怎么做?

我想也许是这样,但它不起作用:

find('input:first', :type => 'checkbox')
Run Code Online (Sandbox Code Playgroud)

And*_*ite 19

假设Capybara.default_selector设置为CSS,那么:

find("input[type='checkbox']")
Run Code Online (Sandbox Code Playgroud)

如果您使用的是XPath,那将会有所不同.

更新(2013年6月):正如@tmg指出的那样,Capybara 2的行为发生了变化.

  • 在Capybara 2中,如果多个元素匹配,find会引发**Capybara :: Ambiguous**.而是使用:first("input [type ='checkbox']")或找到一个更独特的定位器来与find一起使用.见:[first](http://rubydoc.info/github/jnicklas/capybara/master/Capybara/Node/Finders#first-instance_method) (11认同)
  • 是.或者:全部("input [type ='checkbox']") (2认同)

ins*_*ero 11

只是指出tmg找到第一个复选框的正确方法

first("input[type='checkbox']")
Run Code Online (Sandbox Code Playgroud)

如果你想找到第n个复选框(例如第25个):

find(:xpath, "(//input[@type='checkbox'])[25]")
Run Code Online (Sandbox Code Playgroud)

但通常最好使用内部来缩小搜索范围

within 'div.div_class' do
  find("input[type='checkbox']")
end
Run Code Online (Sandbox Code Playgroud)