【问题标题】:Capybara fill_in with xpathCapybara 使用 xpath 填充
【发布时间】:2014-10-21 07:24:25
【问题描述】:

我有多个输入字段,其中一个 ID specification,但它们具有不同的属性。我可以很好地找到它们,但是在弄清楚如何正确使用fill_infind 时遇到了麻烦。目前我有:

find('//input[@id="specification"][@data-number="1"]').fill_in with: "This first sleeping bag should be 0 degrees F"

Error-- Capybara::Webkit::InvalidResponseError:SYNTAX_ERR: DOM Exception 12

谢谢!

【问题讨论】:

    标签: ruby-on-rails-4 xpath rspec capybara


    【解决方案1】:

    首先是fill_in 将在find 返回的元素中查找文本字段。基本上你是在要求 Capybara 找到类似的东西:

    <input id="specification" data-number="1">
      <input type="text">
    </input>
    

    这不太可能存在。

    鉴于您已经找到使用find的文本字段,您想使用set方法输入它:

    find('//input[@id="specification"][@data-number="1"]').set("This first sleeping bag should be 0 degrees F")
    

    假设您没有将 default_selector 更改为 :xpath,由于表达式不是有效的 CSS 选择器,上述操作仍然会失败。您还需要告诉 Capybara 正在使用 :xpath 定位器:

    find(:xpath, '//input[@id="specification"][@data-number="1"]').set("This first sleeping bag should be 0 degrees F")
    

    【讨论】:

    • 我完全忘了提到我试过这个,刚刚又试了一次,我仍然得到同样的错误Capybara::Webkit::InvalidResponseError:SYNTAX_ERR: DOM Exception 12
    • 你的 Capybara.default_selector 设置为 :xpath 了吗?默认为 :css。
    • 不是,但这仍然不起作用:find(:xpath, '//*[id="specification"][@data-number="1"]').set("This first sleeping bag should be 0 degrees F"),它给了我错误Capybara::ElementNotFound:Unable to find xpath "//*[id=\"specification\"][@data-number=\"1\"]"
    • 由于拼写错误[id="specification"],上一条评论中的尝试失败。它缺少@ - 即应该是[@id="specification"]
    • omg...我怎么会这么笨,好吧,太好了,谢谢!
    【解决方案2】:

    具有相同 id 的多个元素是无效的 HTML。你应该从改变它开始。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-04-22
      • 2020-11-20
      • 2014-05-14
      • 1970-01-01
      相关资源
      最近更新 更多