水豚可以找到但不能填写 [英] Capybara can find but not fill_in

查看:72
本文介绍了水豚可以找到但不能填写的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我与水豚有一些非常奇怪的行为。它固执地拒绝填写我的登录表单的字段。

I am having some very strange behaviour with Capybara. It stubbornly refuses to fill in the fields of my login form.

<fieldset>
  <div class="clearfix">
    <label for="user_email">E-Mail Adresse</label>
    <div class="input">
      <input id="user_email" name="user[email]" size="30" type="email" value="">
    </div>
  </div>

  <div class="clearfix">
    <label for="user_password">Passwort</label>
    <div class="input">
      <input id="user_password" name="user[password]" size="30" type="password" value="">
    </div>
  </div>

  <div class="clearfix">
    <div class="input">
      <input name="user[remember_me]" type="hidden" value="0">
      <input id="user_remember_me" name="user[remember_me]" type="checkbox" value="1">
      <label for="user_remember_me">angemeldet bleiben</label>
    </div>
  </div>
</fieldset>

这就是乐趣的开始:

within("#login_form"){ find("#user_email")}                                              
=> #<Capybara::Element tag="input" path="/html/body/div[2]/div[@id='content']/div/div[1]/form[@id='login_form']/fieldset/div[1]/div/input[@id='user_email']">
within("#login_form"){ fill_in("#user_email", with: "foo@example.com")}                  
Capybara::ElementNotFound: Unable to find field "#user_email"

我不太了解如何找到而不是找到元素。
对此,我们将不胜感激。

I don't quite understand how it is possible to be able to find, and yet not find, an element. Another pair of eyes on this would be appreciated.

推荐答案

find <的定位器/ code>和 fill_in 不同:


  • 发现 -当第一个参数不是符号,它假定为Capybara.default_selector-即CSS选择器或xpath。

  • fill_in -第一个参数是字段的名称,ID或标签文本。 / li>
  • find - When the first parameter is not a symbol, it is assumed to be the Capybara.default_selector - ie a css-selector or xpath.
  • fill_in - The first parameter is the field's name, id or label text.

字符串 #user_email表示CSS选择器。这就是为什么它可以在查找而不是 fill_in 的情况下工作的原因。

The string "#user_email" represents a css-selector. This is why it works in find but not fill_in.

要使 fill_in 工作,您只需传递id值-即 user_email即可。

For fill_in to work, you need to just pass in the id value - ie just "user_email".

within("#login_form"){ fill_in("user_email", with: "foo@example.com")}  

这篇关于水豚可以找到但不能填写的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆