选择表格上的排序按钮

时间:2016-09-01 19:53:43

标签: ruby capybara

我有一个表,我需要选择升序和降序排序按钮。 html看起来像这样

<table id="results" class="list-table" width="100%" cellspacing="0" cellpadding="0">
<tbody>
<tr class="header-row ">
<th>
<th id="field_app_job_title" class="app_job_title ">
<th id="field_app_full_name" class="app_full_name ">
<th id="field_app_individual_workflow_states"   class="v0_workflow_state_definition_id_sort ">
<th id="field_app_workflow_state_entrance_reason"  class="v0_workflow_state_entrance_reason_sort ">
<span class="controls">
<span class="move_left"></span>
<span class="move_right" style="display: none;"></span>
<span class="sort_asc"></span>
<span class="sort_desc"></span>
<span class="remove"></span>
</span>
Workflow State Entrance Reason
</th>
<th class="action-menu"></th>
</tr>

我尝试了以下内容:

within_table  'results' do
  find('Workflow State Entrance Reason').click_link('sort_asc')
end

within_table  'results' do
  find('v0_workflow_state_entrance_reason_sort').click_link('sort_asc')
end

甚至是这个,但到目前为止还没有找到该元素。

within_table  'results' do
  ('input[id^="field_app_workflow_state_entrance_reason"]').click_link('sort_asc')
end

建议?

3 个答案:

答案 0 :(得分:0)

您应该使用CSS选择器find元素,然后单击它。使用这样的东西:

find(".sort_asc").click

您还应该检查似乎无效的HTML代码(您已经禁止th个选择器。)

答案 1 :(得分:0)

鉴于您的html(希望得到修复),以下内容应该点击您要点击的内容

within_table 'results' do
  find('.v0_workflow_state_entrance_reason_sort .sort_asc').click
end

您尝试不起作用的原因在

之下
within_table  'results' do
  # find with no selector type specified takes a CSS selector
  # not random text and there is no link (<a> element in your html)
  find('Workflow State Entrance Reason').click_link('sort_asc')        
end

within_table  'results' do
  # v0_workflow... is not the correct CSS selector (unless that is the tag_name of the element you're looking for) and there is no link in your html
  find('v0_workflow_state_entrance_reason_sort').click_link('sort_asc')
end

within_table  'results' do
  #there is no link in your html
  find('input[id^="field_app_workflow_state_entrance_reason"]').click_link('sort_asc')
end

答案 2 :(得分:0)

感谢大家的投入,并对我迟到的回复感到抱歉。你的回答让我走上了正确的道路,我能够解决这个问题。一个问题是我没有意识到排序元素直到鼠标悬停在柱子上才出现。

within_table 'results' do
  find('#field_app_workflow_state_entrance_reason', text: 'Workflow State Entrance Reason').hover
end

然后在我收到此错误时出现了新错误

  

Capybara :: Poltergeist :: MouseEventFailed:点击一下即可   坐标[719.5,438]失败了。恶作剧者发现了另一个元素   使用CSS选择器&#39; html body#job_applications.search div#container   section#main div #main-content section.details div.subpage   section#search_results table#results.list-table tbody tr.header-row   #field_app_workflow_state_entrance_reason.v0_workflow_state_entrance_reason_sort span.controls&#39;在这个位置。它可能与你的元素重叠   正试图与之互动。如果你不关心重叠   元素,尝试使用node.trigger(&#39;点击&#39;)。

对node.trigger和我的一些研究能够点击这样的元素而不会得到错误。

within_table 'results' do
  find('.v0_workflow_state_entrance_reason_sort span.controls .sort_asc').trigger("click")
end

我不确定这是不是一个好主意,但它现在似乎有效。