【问题标题】:Capybara: Pulling in multiple Radio button optionsCapybara:引入多个单选按钮选项
【发布时间】:2016-09-21 22:58:14
【问题描述】:

我遇到了一个问题,我认为问题在于我的 page.all 是如何拉出单选按钮问题的。

所以这是表格本身的 HTML(多个问题,每个问题有 5 个单选按钮选项):

    <table class="table table-striped table-stuff table-collapsible">
    <colgroup>
    <thead>
    <tbody>
    <input id="0_answer_question_id" value="9966" name="response[answers][0][answer_id]" type="hidden">
    <tr>
    <td class="heading">
    <td class="option">
    <div class="radio-inline radio-inline--empty">
    <input id="question_1_1" value="1" name="response[answers_attributes][0][answer_opinion]" type="radio">
    <label for="question_1_1">Strongly Disagree</label>
    </div>
    </td>
    <td class="option">
    <td class="option">
    <td class="option">
    <td class="option">
    </tr>
    <input id="response_1_question_id" value="9966" name="response[answers_attributes][1][answer_question_id]" type="hidden">
    <tr>
    <input id="response_1_id" value="<a number>" name="response[answers_attributes][1][id]" type="hidden">
   <Same as above repeated 5 times with numbers changed>
    </tbody>
    </table>

我正在使用:

page.all('table.table-stuff tbody tr', minimum: 6).each do |row|
  row.all("td label").sample.trigger('click')
end

获取每一行并从中选择一个。但是,我注意到“有时”一行不会被选中。我的理论是“标题”(它本身有一个

当一个表被命名为table table-striped table-stuff table-collapsible...你怎么知道实际的表“名称”是什么? (我没有写这个网站,只是为它做测试)。当把它放在page.all('table.&lt;etc&gt;')?

【问题讨论】:

    标签: ruby-on-rails capybara


    【解决方案1】:

    如果标题 td(在您的示例中未展开)还包含一个标签元素(因此它将包含在您的 all 调用的结果中),那么您只需要更改 CSS 选择器,这样它就不会被包括在内 - 类似

    row.all("td.option label").sample.trigger('click') # only choose labels contined in tds with the class of 'option'
    

    row.all("td:not(.heading) label").sample.trigger('click') # choose labels contained in tds without the class of 'heading'
    

    关于表名的第二个问题,我不太明白你在问什么。表格没有名称属性,它们可以有一个 id 属性或包含一些文本的标题,然后可以通过 find(:table, 'id or caption text')within_table('id or caption text') { code to execute within scope of the table } 使用水豚找到它们。相反,您似乎在谈论在 CSS 选择器中使用“.”指定的元素上的类。因此,将表格元素与您列出的所有类匹配的 CSS 选择器将是 - 'table.table.table-striped.table-stuff.table-collapsible'

    注意:如果您确定始终只有 5 个选项,您可以将 :count 选项添加到您的 find 中,以确保您的选择器只找到这些项目

    row.all("td.option label", count: 5).sample.trigger('click')
    

    【讨论】:

    • 这最终奏效了,因为事实上标题确实有一个标签选项。 (至少我在学习为什么事情不起作用哈哈)。您的表格答案确实回答了我的问题,看起来它们只是多个类。
    猜你喜欢
    • 1970-01-01
    • 2018-08-19
    • 2016-05-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-10-04
    相关资源
    最近更新 更多