How to click on the second link with the same text using Capybara in Rails 3?

前端 未结 7 925
北海茫月
北海茫月 2020-12-24 11:25

In a view file I have:

= link_to \'View\', post
= link_to \'View\', comment

In a spec file (I\'m using Capybara):

click_on          


        
7条回答
  •  死守一世寂寞
    2020-12-24 12:08

    There are many ways for solving this type of problems.
    Do it like this

    if(page.find("a")[:href] == "comment")
        click_on("View")
    

    or

    page.find("a:eq(2)").click
    

    Remember javascript indexing starts with 0 while In Capybara, indexing starts with 1. So use a:eq(2) here for second href.

提交回复
热议问题