7

How can i click a link using capybara.I need to click the graduation link

<a href="/arts?Occasion=Graduation&amp;top_menu_item_title=+-+Graduation">Graduation</a>

I used following code .but showing error message undefined method click

 page.find(:link,"Graduation").click

also used Xpath

page.find(:xpath, "//a[@href='/arts?Occasion=Graduation']").click

it is also not working

But my problem is my page contains multiple links of graduation.

so using page.all(:link,"Graduation") returns Ambiguous match, found 2 elements matching link "Graduation"

divz
  • 7,847
  • 23
  • 55
  • 78
  • Look at this - http://stackoverflow.com/questions/14513377/how-to-click-first-link-in-list-of-items-after-upgrading-to-capybara-2-0/14517076#14517076 – Andrei Botalov Jul 31 '13 at 07:03

2 Answers2

11
page.all(:link,"Graduation") returns Ambiguous match, found 2 elements matching link "Graduation"

That means page contains multiple graduation text link.so if you want to click the first one you can write like this

  page.all(:link,"Graduation")[0].click

It will click the first link and the below one click the second link.You can use anyone as your needs.

  page.all(:link,"Graduation")[1].click

am sure this will work for you

Psl
  • 3,830
  • 16
  • 46
  • 84
3

First try using it without the page part. I've never needed it myself

Then you can try and find the link with the css for the exact link.

find("a[href='/arts?Occasion=Graduation&amp;top_menu_item_title=+-+Graduation']").click
Martin Larsson
  • 1,008
  • 1
  • 8
  • 25