Achyuth Pydmarri Achyuth Pydmarri - 4 months ago 8
Ruby Question

Capybara - finding an element with Id & text or using multiple properties

I have 2 links with same id 2 and different text

<li class="">
<a href="#" id="2">2</a>
</li>

<li class="">
<a href="#" id="2">Next</a>
</li>


now i wanted to write something like

page.find_by_id("2").has_text?("2")


but it is not working not sure how to find the element with id = "2" & text ="2"

I am getting error

Capybara::Ambiguous: Ambiguous match, found 2 elements matching id "2"

Answer

To get the last link with id "2":

page.all(:css, "a[id='2']").last()

To get the first link with id "2" and text "Next":

page.find(:xpath, "//a[@id='2'][text()='Next']")

To get the first link with text "Next":

page.find_link("Next")
Comments