Unable to find field "Name" (Capybara::ElementNotFound)

17,267

Solution 1

It looks like to me that you are looking for a field label Name, but your name field does not have a label, so you probably need to use the ID of the field or the name which is probably:

"#{resource_name}[name]"

Also, as @nmott said in his comment, you should try using save_and_open_page so that you can actually look at the page, However, be aware you need the launchy gem to use this method.

Furthermore, what you might discover is you're not even on the right page. My usual problem when testing pages OTHER than the sign up page is that I've been redirected and didn't know it. So after using visit, you should also use assert_template to make sure you're on the right page.

Solution 2

I encountered this issue and realized that Capybara filtered out hidden fields--my element belonged to a non-active tab (hidden) in a multi-tab page. I just passed the :visible arg and set it to false, and voila! the element was found.

fill_in "content", with: 'foo bar', visible: false

or

find('#item_content', visible: false).set 'foo bar'

Solution 3

I have the same problem myself and I had to stop using fill_in all together. What I did was replacing all occurrences of fill_in with the following code :

    element = page.find("Name")
    element.set(@visitor[:name])

I guess you can encapsulate this into a method in order to make your tests more smooth

Solution 4

Try to narrow down your scope from within the form as such

within("form#whatever_form_id") do
  fill_in "Name", :with => @visitor[:name]
  click_button "Sign up"
end
Share:
17,267

Related videos on Youtube

Andre Tachian
Author by

Andre Tachian

Updated on September 15, 2022

Comments

  • Andre Tachian
    Andre Tachian over 1 year

    I'm trying to use capybara+rspec and get this error: Unable to find field "Name" (Capybara::ElementNotFound)

    Here is my form:

    %h2 Sign up
    = simple_form_for(resource, :as => resource_name, :url => registration_path(resource_name), :html => {:class => 'form-vertical' }) do |f|
      = f.error_notification
      = display_base_errors resource
      = f.input :name, :autofocus => true
      = f.button :submit, 'Sign up', :class => 'btn-primary'
    = render "devise/shared/links"
    

    Here is my user_steps.rb

    When /^I sign up with valid user data$/ do
      create_visitor
      sign_up
    end
    
    def create_visitor
      @visitor ||= { :name => "Test visitor"}
    end
    
    def sign_up
      visit '/users/sign_up'
      fill_in "Name", :with => @visitor[:name]
      click_button "Sign up"
    end
    

    What's wrong????

    • thank_you
      thank_you over 11 years
      When an element is not found it could denote that either you didn't write the element or your test is looking at the wrong page. I would first check the page source of /users/sign_up to see if you matched the appropriate name or id to your test. If that's right, then I would check to see if there are any obstructions the test might have when reaching /users/sign_up.
    • nmott
      nmott over 11 years
      Drop in save_and_open_page just after your fill_in and you will be able to see what Capybara actually thinks is on the page.
  • Arslan Ali
    Arslan Ali about 9 years
    Isn't find method used for finding CSS?
  • Justin
    Justin over 8 years
    A second time I just had this error it was all caused by capitalisation of the first letter of words.
  • flame3
    flame3 almost 8 years
    This helped us to save our time. Thanks a lot!