如何在没有 class 或 id 的字段集中查找元素

How to find element within fieldset with no class or id

我的页面上有两个字段集。 None 其中有任何 id 或 class。 现在我想在第二个字段集中填写特定字段。

目前我正在做这样的事情,但没有用:

within_fieldset('fieldset') do
  fill_in 'app_answers_attributes_0_answer', with: 'My First Answer'
end
click_on 'Submit'

它给出了错误:

Capybara::ElementNotFound: Unable to find fieldset "fieldset"

知道怎么做吗?

within_fieldset 获取字段集的 ID 或图例文本 - http://www.rubydoc.info/gems/capybara/Capybara/Session#within_fieldset-instance_method - 所以 within_fieldset('fieldset') 不适合你也就不足为奇了。如何做自己想做的事实际上取决于 HTML 的结构。例如,如果您的字段集有图例

<fieldset>
  <legend>Something</legened>
  ...
</fieldset>
<fieldset>
  <legend>Other thing</legened>
  ...
</fieldset>

你可以做到

within_fieldset('Other thing') do
    ...
end 

如果您没有图例,但有环绕元素

<div id="first_section">
   ...
    <fieldset>...</fieldset>
<div>
<div id="second_section">
    <fieldset>...</fieldset>
</div>

然后您可以使用 CSS 将范围限定到正确的字段集

within('#second_section fieldset') do
   ...
end

如果字段集是兄弟姐妹

<div>
  <fieldset>...</fieldset>
  <fieldset>...</fieldset>
</div>

然后您可以使用 CSS 同级选择器

将范围限定到第二个字段集
within("fieldset + fieldset") do
  ...
end