user664833
user664833

Reputation: 19525

click on second ambiguous link with Capybara

I want to click on the second of the two following links:

<a href="photo/1">photo</a>
<a href="photo/2">photo</a>

As you can see, the URLs are actually different, but the link title, "photo", is the same in both. I am not using IDs on my links, and there is no nesting class context by which to distinguish the links.

Ideally, I would like to be able to click even a third, or nth ambiguous link.

An example of a DSL for this purpose would be something like:

click_link photo.title, match: 2

Upvotes: 2

Views: 2166

Answers (2)

Justin Ko
Justin Ko

Reputation: 46846

I find that using all is easier to read than locating by xpath:

all('a', :text => 'photo')[1].click

However, it not as fast as using xpath.

Upvotes: 3

Brad Werth
Brad Werth

Reputation: 17647

You should be able to use xpath for this. Something like:

find(:xpath, '//a[text()='photo'][2]').click

Upvotes: 1

Related Questions