How to Click Text Using Capybara and Cucumber

Out of the box, Cucumber contains a web step that allows you to easily click a link. But what if what you want to click is not a link, but some plain text like:

Click me

Unfortunately, Cucumber doesn’t have a web step for such case, so Dustin and I had to do some digging around. We found in Capybara’s source code that Capybara’s Node::Finders::find() takes the same options as Node::Finders::all(), and it supports :text as an argument!

So, this is the Capybara code that would click the text above.

find('.clickable-text', :text => 'Click me').click
  • Torey Heinz says:

    This worked great for me thanks!

  • Comments are closed.