Posted over 4 years ago. Visible to the public. Repeats.

Capybara will not find links without an href attribute

Capybara will fail to find <a> tags that are missing an href attribute. This will probably happen to you every now and then on JavaScript-heavy applications.

An example would be an AngularJS application where the following HTML actually works. [1]

<a ng-click="hello()">Hello</a>

Capybara will fail to find that link, even though looking it up via the DOM shows it:

>> find_link("Hello") Capybara::ElementNotFound: Unable to find link "Hello" >> find("a").text => "Hello"

To make find_link and click_link work, simply add href="#" to your element:

<a href="#" ng-click="hello()">Hello</a>
>> find_link("Hello") => #<Capybara::Element tag="a">

[1] Note that while it does work in your application (links are usually styled correctly, and Angular will take care of the click event), the HTML spec defines hyperlinks as anchor tags (<a>) that have an href attribute. Without an href they are just link placeholders. So Capybara at least adheres to the HTML spec. :)

Does your version of Ruby on Rails still receive security updates?
Rails LTS provides security patches for old versions of Ruby on Rails (3.2 and 2.3).

Owner of this card:

Arne Hartherz
Last edit:
about 2 years ago
About this deck:
We are makandra and do test-driven, agile Ruby on Rails software development.
License for source code
Posted by Arne Hartherz to makandra dev
This website uses cookies to improve usability and analyze traffic.
Accept or learn more