【问题标题】:How to fill in input field that is the child of an element that sits next to an element with given text如何填写作为具有给定文本的元素旁边的元素的子元素的输入字段
【发布时间】:2014-08-20 15:39:21
【问题描述】:

在 capybara 测试中,我需要填写一个没有唯一 id 或 class 属性的文本字段。 文本字段(本例中称为标题的字段)可以出现在页面的任何位置。我们唯一知道的是文本字段被包裹在一个 div 中,这个 div 紧跟在一个 h3 标签之后,它的内容是“Title”

<h3>Title</h3>
<div class="input-row clear">
  <input id="ember5046" class="ember-view ember-text-field" type="text">
</div>
<h3>Work Location</h3>
<div class="input-row clear">
  <input id="ember5048" class="ember-view ember-text-field" type="text">
</div>

我该怎么做?

我们不允许使用 xpath(公司政策)

我们不允许像 all("input")[0] 那样做基于索引的选择器

【问题讨论】:

  • "我们不允许使用 xpath" 为什么?另外 - 你为什么不问谁制定了这个愚蠢的规则来解释你如何做到这一点;)
  • 第二 - 你有没有办法改变 html?如果你可以在周围的 div 中放置一个唯一的 id/class,那么你就可以做到
  • 如果您决定允许 XPath,那么今天有 another question 遇到了完全相同的问题。该问题包括 XPath 解决方案。
  • find('#id').set 'text' 假设您的 id 不是在加载时唯一生成的

标签: ruby-on-rails rspec capybara integration-testing


【解决方案1】:

我认为使用 css 相邻兄弟组合选择器应该是可能的。比如:

h3[text=Title] + div

这将为您提供与 + 之后的事物匹配的下一个元素。如果您希望所有匹配的内容都可以使用 ~ 运算符。这些兄弟选择器只能帮助你找到某个节点之后的东西。据我所知,倒退是不可能的。

【讨论】:

    猜你喜欢
    • 2014-05-07
    • 2019-08-31
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多