开发者

How do I find the first checkbox in Capybara?

I'd like to find the first instance of a checkbox 开发者_C百科using the capybara dsl. Anyone know how to do that?

I thought perhaps it'd be this, but it didn't work:

find('input:first', :type => 'checkbox')


Assuming Capybara.default_selector is set to CSS then:

find("input[type='checkbox']")

If you're using XPath it will be different.

Update (June 2013): as @tmg points out, the behaviour for Capybara 2 has changed.


Just to point out tmg's right way to find the first checkbox

first("input[type='checkbox']")

If you want to find n-th checkbox (25-th for example):

find(:xpath, "(//input[@type='checkbox'])[25]")

But it's often better to use within to narrow your searching area

within 'div.div_class' do
  find("input[type='checkbox']")
end


The least flaky way to find the first checkbox could be:

find("input[type='checkbox']", match: :first)
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜