user3897533
user3897533

Reputation: 477

Picking the first element using xpath in capybara

I have the following line of code

link = find(:xpath, "//div[@id='tree']//a[contains(.,'#{peril}')]")

Above step yields in two elements. How do I pick the first one.

I am getting a Ambiguous match found 2 elements matching xpath. Here is the HTML "    ShipCase_US_MortalityRatingGroup_Life   Portfolio result   Earthquake   Infectious Disease"

Upvotes: 2

Views: 2169

Answers (2)

Salomanuel
Salomanuel

Reputation: 955

find(".active", match: :first).click

this solution uses Capybara's (quite important) waiting capabilities

Upvotes: 3

Daniel Haley
Daniel Haley

Reputation: 52858

You need to surround the entire XPath in parentheses and add the [1] after it.

(//div[@id='tree']//a[contains(.,'#{peril}')])[1]

Upvotes: 3

Related Questions