Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I find the first checkbox in Capybara?

I'd like to find the first instance of a checkbox 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')
like image 681
btelles Avatar asked May 18 '11 14:05

btelles


3 Answers

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.

like image 182
Andy Waite Avatar answered Nov 07 '22 19:11

Andy Waite


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
like image 23
installero Avatar answered Nov 07 '22 19:11

installero


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

find("input[type='checkbox']", match: :first)
like image 2
Justin Tanner Avatar answered Nov 07 '22 19:11

Justin Tanner