为什么水豚在查找相同元素时不允许 fill_in?
Why would Capybara not allow fill_in when find for the same element works?
当我找到带有
的元素时
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">
有效。
但是当我尝试使用 fill_in
时,我得到:
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'
查找使用了完整选择器,但 fill_in 使用了字段名称、标签或 ID。
所以在这种情况下 fill_in('auto_policy_drivers_attributes_0_last_name', with: 'aaa')
有效
你应该使用
fill_in "the id/name/label of the field you want to fill", :with => "some text"
例如:
fill_in 'user_username', :with => 'john123'
find
使用 css 路径或 xpath,具体取决于您的 Capybara.default_selector
,例如:
find(:xpath, '//*[@id="username"]')
或
find(:css, '#content > div.content-inner.clearfix > ul > li > a')
当我找到带有
的元素时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">
有效。
但是当我尝试使用 fill_in
时,我得到:
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'
查找使用了完整选择器,但 fill_in 使用了字段名称、标签或 ID。
所以在这种情况下 fill_in('auto_policy_drivers_attributes_0_last_name', with: 'aaa')
有效
你应该使用
fill_in "the id/name/label of the field you want to fill", :with => "some text"
例如:
fill_in 'user_username', :with => 'john123'
find
使用 css 路径或 xpath,具体取决于您的 Capybara.default_selector
,例如:
find(:xpath, '//*[@id="username"]')
或
find(:css, '#content > div.content-inner.clearfix > ul > li > a')