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

I have been trying to fix this error for a very long time, and I cannot understand what it is. Thanks in advance for your help!

I am writing tests for a rails application with capybara and rspec trying to click an element in the navigation bar and then check that the page is correct, but it seems that no matter how I tell capybara to click the element, it cannot 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(...) 

etc., as well as with and without: js and in different "inside" operations, but I always get Capybara :: ElementNotFound: Unable to find css "a". I also tried to increase the default timeout of capybara to 5 seconds.

This is the element I'm trying to click (according to the source code of the page)

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

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

Thank you, ton, I really appreciate it!

+6
source share
2 answers

Your code must be correct, I always used click_link 'Foobar' . You tried to add

 save_and_open_page 

after the line visit ? It generally helps me understand why a capybara cannot find things.

+9
source

It turns out that some of the links did not appear because the page I also did not load properly, because there was no data in it. I saw this when I opened it on the localhost server, but save_and_open (as suggested by Ju Liu, thanks!), I probably would have done the same. I had to either start from another page, loaded properly, or add to some factories and fill the page.

0
source

Source: https://habr.com/ru/post/948102/


All Articles