Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Capybara::ElementNotFound: Unable to find css "a"

I have been trying to fix this error for a very long time now and I can't seem to figure out what it is. Thanks in advance for your help!

I am writing tests for a rails app with capybara and rspec trying to click on an element in a nav bar and then test to make sure the page is correct, but it seems no matter how I tell capybara to click the element, it can't find it.

   ...
  subject { page }

  describe 'when clicking Home link', :js do
    before do
      visit some_path
      find('a', text: "Home").click
    end
    it { should have_selector('title', text: 'bla bla bla') }
  end
  ...

I have tried many different versions of this code, including

  click_link('Home'),
  click_on('Home'),
  find('a.header-link.header-link-home', text: 'Home').click,
  find_link(...).click,
  click_button(...)

and so on, as well as with and without :js, and in different "within" statements, but I always get Capybara::ElementNotFound: Unable to find css "a". I have also tried increasing capybara's default wait time to 5 seconds.

This is the element that I am trying to click (according to the page's source code)

  <a class="header-link header-link-home" href="/">Home</a>

Gem versions: capybara (1.1.4), rspec (2.12.0)

Thanks a ton, I really appreciate it!

like image 697
Collin Avatar asked Jun 26 '13 00:06

Collin


1 Answers

Your code should be correct, I've always used click_link 'Foobar'. Have you tried to add a

save_and_open_page

after the visit line? It generally helps me a lot to understand why capybara can't find stuff.

like image 71
Ju Liu Avatar answered Sep 21 '22 14:09

Ju Liu