Achyuth Pydmarri Achyuth Pydmarri -5 years ago 134
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 class="">
<a href="#" id="2">Next</a>

now i wanted to write something like


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 Source

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":

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download