regedarek regedarek - 5 months ago 34
Ruby Question

How to click first link in list of items after upgrading to Capybara 2.0?

How to click first link in that case:

<div class="item">
<a href="/agree/">Agree</a>
</div>
<div class="item">
<a href="/agree/">Agree</a>
</div>


within ".item" do
first(:link, "Agree").click
end


and I get this error:

Capybara::Ambiguous:
Ambiguous match, found 2 elements matching css ".item"


And without the
within
I get this error:

Failure/Error: first(:link, "Agree").click
NoMethodError:
undefined method `click' for nil:NilClass

Answer

You can just use:

first('.item').click_link('Agree')

or

first('.item > a').click

(if your default selector is :css)


Code in your question doesn't work as:

within ".item" do
  first(:link, "Agree").click
end

is equivalent to:

find('.item').first(:link, "Agree").click

Capybara finds several .item's so it raises an exception. I consider this behavior of Capybara 2 very good.

Comments