table 上的选择排序按钮

Selecting sort buttons on table

我有一个 table 需要 select 排序升序和降序按钮。 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

甚至这个,但是 none 到目前为止已经找到了元素。

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

建议?

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

find(".sort_asc").click

您还应该检查您的 HTML 代码,它似乎无效(您使用了 th 选择器)。

考虑到您的 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

感谢大家的意见,对于我迟到的回复深表歉意。您的回答使我走上了正确的道路,并且我能够解决这个问题。我没有意识到的一个问题是,直到鼠标悬停在该列上时排序元素才出现。

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

然后在我收到此错误的地方出现了一个新错误

Capybara::Poltergeist::MouseEventFailed: Firing a click at co-ordinates [719.5, 438] failed. Poltergeist detected another element with CSS selector '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 th#field_app_workflow_state_entrance_reason.v0_workflow_state_entrance_reason_sort span.controls' at this position. It may be overlapping the element you are trying to interact with. If you don't care about overlapping elements, try using node.trigger('click').

对 node.trigger 进行了一些研究,我能够像这样单击元素而不会出现错误。

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

我不确定这是否是个好主意,但它目前似乎可行。