我正在尝试填写标记为id =“admin_passsword”的两个文本框。我可以轻松访问第一个,但由于除了占位符和上面的标签之外没有其他差异,我不知道如何访问第二个文本框来填充该字段。
<div class="fields">
<div class="field-row">
<label for="admin_email">Email</label>
<input autocapitalize="off" id="admin_email" name="admin[email]" placeholder="Email" size="30" type="text" value="">
</div>
<div class="field-row">
<label for="admin_serial_number">Serial number</label>
<input id="admin_serial_number" name="admin[serial_number]" placeholder="Serial Number" size="30" type="text">
</div>
<div class="field-row">
<label for="admin_password">Password</label>
<input id="admin_password" name="admin[password]" placeholder="Password" size="30" type="password">
</div>
<div class="field-row">
<label for="admin_password_confirmation">Password confirmation</label>
<input id="admin_password" name="admin[password]" placeholder="Confirm Password" size="30" type="password">
</div>
</div>
答案 0 :(得分:3)
使用Capybara 2.1,这有效:
fill_in("Password", with: '123456', :match => :prefer_exact)
fill_in("Password confirmation", with: '123456', :match => :prefer_exact)
首选上述评论的链接:: prefer_exact是Capybara 1.x中的行为。如果找到多个匹配,其中一些是精确的,而其中一些不匹配,则返回第一个eaxctly匹配元素。
答案 1 :(得分:2)
有多种方法可以做到这一点。以下是其中一些:
在Capybara 2.0中你可以做到,例如这样:
all('label[for=admin_password_confirmation]').each {|field| field.set('123456')}
Capybara 2.1支持新的:exact
选项:
fill_in('Password', with: '123456', exact: true)
fill_in('Password Confirmation', with: '123456', exact: true)
但我建议你设置:
# e.g. in features/support/env.rb
Capybara.configure do |config|
config.exact = true # exact will be true by default in option hashes. It's false by default
end
# in your tests
fill_in('Password', with: '123456')
fill_in('Password Confirmation', with: '123456')
答案 2 :(得分:1)
你也可以在街区内做...
within('.class div:nth-child(div you want) do
fill_in("css", :with => "text")
end