Jump to content

The ultimate community for Ruby on Rails developers.


select a specific url (with the same text) in a list using capybara

capybara cucumber rails4

  • Please log in to reply
2 replies to this topic

#1 Vell



  • Members
  • 83 posts
  • LocationWashington, DC

Posted 24 April 2014 - 02:34 PM

Hello all,


I can't seem to figure out how to select a specific url in a list of urls that have the same text and no id. Even if I give the urls an id, I am still having trouble clicking the url by id. I was wondering how am I able to do one or the other or both.


As a small example.


In my rails template I may have something like:

<% @records.each do |record| %>
  <%= link_to "edit", edit_record_path(record) %>
<% end %>

Which may generate something like:

<a href="/record_id=1">edit</a>
<a href="/record_id=2">edit</a>
<a href="/record_id=3">edit</a>

In my cucumber test where I am using capybara, I would want to click a url that isn't the first one and I have not been able to figure it out. Even with googling and reading the node finder reference.


I know that if I want to select the first one, it's as easy as:

click_on 'edit', match: :first

But something like `match: :last` doesn't seem to exist or even :second. I can understand why :second wouldnt' exist.


But if I wanted to click on the second or third edit in the list how would I go about this. The closest I have gotten to this is using giving the urls an id like record_# and using the find_by_id method like so:

link = page.find_by_id "record_#{record.id})

This gives me a:

[#<Capybara::Element tag="a" path="/html/body/table/tbody/tr[2]/td[4]/a">]

But I can't seem to figure out how to use this along with the click_on method to actually move on with my test.


Any suggestions or help would be appreciaited.



#2 Ohm



  • Moderators
  • 507 posts
  • LocationCopenhagen

Posted 25 April 2014 - 06:23 AM

I'm guessing now, but couldn't you just do something like

link = page.find_by_id("record_#{record.id}")

or even



Blog: http://ohm.sh | Twitter: @madsohm | Work: Lokalebasen.dk

#3 Vell



  • Members
  • 83 posts
  • LocationWashington, DC

Posted 25 April 2014 - 05:40 PM

Hey Ohm,


I wasn't aware that there was a .click method I could use if I found a URL by id. I ended up doing this in a very round about way so that I did not have to rely on finding the ID.


What I ended up doing was passing in the text of the url that had multiple instances and the index of which url I wanted to click on from the array that was returned. The good part is that it doesn't matter if the url has an id or doesnt. The downside is, I have to know which index to pass in.

 select_link_and_go("email group", Model.count - 1) #in the case of wanted to click on the last URL.
  def select_link_and_go(text, index)
    links = page.all('a', text: text)
    if links.size == 1
      visit links.first[:href]
      visit links[index][:href]

and now that you have told me about the .click option I have changed my method from visiting the path returned by :href to clicking on the object directly in the array.




Of course I have to think even with this, there is a better way to write it.

Also tagged with one or more of these keywords: capybara, cucumber, rails4

0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users