Reputation: 477
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
Reputation: 955
find(".active", match: :first).click
this solution uses Capybara's (quite important) waiting capabilities
Upvotes: 3
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