Michael Durrant
Michael Durrant

Reputation: 96484

Why would Capybara not allow fill_in when find for the same element works?

When I find the element with

pry(#<RSpec::Core::ExampleGroup::Nested_1::Nested_2>)>
find('input#auto_policy_drivers_attributes_0_last_name')
=> #<Capybara::Element tag="input" path="/html/body/section/div[1]/form/div[4]/div[2]/div[2]/div[2]/input">

it works.

but when I try and use fill_in I get:

pry(#<RSpec::Core::ExampleGroup::Nested_1::Nested_2>)>
fill_in('input#auto_policy_drivers_attributes_0_last_name', with: 'aaa')
Capybara::ElementNotFound: Unable to find field "input#auto_policy_drivers_attributes_0_last_name"
from /Users/mdurrant/.rvm/gems/ruby-2.1.5/gems/capybara-2.4.4/lib/capybara/node/finders.rb:41:in `block in find'

Upvotes: 0

Views: 339

Answers (2)

fabdurso
fabdurso

Reputation: 2444

You should use

fill_in "the id/name/label of the field you want to fill", :with => "some text"

for example:

fill_in 'user_username', :with => 'john123'

find works with the css path or the xpath, depending on your Capybara.default_selector, for example:

find(:xpath, '//*[@id="username"]')

or

find(:css, '#content > div.content-inner.clearfix > ul > li > a')

Upvotes: 2

Michael Durrant
Michael Durrant

Reputation: 96484

find used a full selector but fill_in uses a field name, label or ID.

So in this case fill_in('auto_policy_drivers_attributes_0_last_name', with: 'aaa') works

Upvotes: 1

Related Questions