How to click on the second link with the same text using Capybara in Rails 3?

Aleksandr Shvalev picture Aleksandr Shvalev · Jul 18, 2011 · Viewed 38.8k times · Source

In a view file I have:

= link_to 'View', post
= link_to 'View', comment

In a spec file (I'm using Capybara):

click_on 'View'

It clicks on the first link, but I want it to click on the second one. How can I do it?

Answer

Gabriel F. Engel picture Gabriel F. Engel · Apr 13, 2012

You could try to find all entries and deal with an array:

page.all('a')[1].click

Would help to have a class or use within to scope your search ;)