How to click on image as a link using RSpec and Capybara

I'm using Rspec and Capybara.

How can I write a step to click on an image? I have been unable to find a good way to get Capybara to see the image as a link.


Example of html

<a title="inGroups" href="/de/users/index/roles-list/serviceID/1112/inGroup/1">
  <img border="0" alt="inGroups" src="/themes/system/images/icon/16/role_activate.png">

<a title="inGroups" href="/de/users/index/roles-list/serviceID/1114/inGroup/1">
   <img border="0" alt="inGroups" src="/themes/system/images/icon/16/role_activate.png">

<a title="notInGroups" href="/de/users/index/roles-list/serviceID/1112/inGroup/0">
   <img border="0" alt="notInGroups" src="/themes/system/images/icon/16/role_deactivate.png">

<a title="notInGroups" href="/de/users/index/roles-list/serviceID/1114/inGroup/0">
    <img border="0" alt="notInGroups" src="/themes/system/images/icon/16/role_deactivate.png">

Any suggestions?


It shouldn't be any different to clicking a normal link. I don't see how you would go about selecting an individual link in your example though, without any uniquely identifiable attributes on the link.

If you can't change the markup, you may need to use XPath to actually select the href element by the URL e.g.

my_link = find(:xpath, "//a[contains(@href,'1114/inGroup/1')]")

Would give you the first link, and then

Should click it as normal.

EDIT: Just to clarify, the important thing is that you select the a element and click it, not the img.

Need Your Help

Can I run multiple instances of the Ruby interpreter in the same process?

c++ c ruby

I have an application which needs to run Ruby code interfacing with the application itself as a kind of extension. The problem is, I need to be able to run separate instances of the Ruby interpreter

Python: find out whether a list of integers is coherent

python sequence itertools

I am trying to find out whether a list of integers is coherent or 'at one stretch', meaning that the difference between two neighboring elements must be exactly one and that the numbers must be