单击包含xpath和cucumber的表中的编辑链接

时间:2010-05-10 19:35:00

标签: xpath cucumber

如何使用黄瓜点击表格中项目列表中的编辑链接?使用xpath?

我有一个选择器工作,返回我需要的链接,但它是许多结果之一......我怎么能只返回1个结果?

这是我的黄瓜步骤

When /^I click the "([^\"]*)" link for "([^\"]*)"$/ do |link, cell_value|
  within "//*[.//td[contains(.,'#{cell_value}')] and .//a[text()='#{link}']]" do |scope|
    scope.click_link link
  end
end

正如您所看到的,这将有效,但只需点击表格中的第一个编辑链接,即使它知道其他匹配的链接......这样也不行。

以下是表格和选择器:http://pastie.textmate.org/private/9od335pmsj4hi9nbe9lbow

谢谢!

这是该文件的来源,以防服务中断或链接到外部代码不受欢迎。

## HTML source table

<table>
  <tr>
    <th>
      Name
    </th>
    <th>
      Enabled
    </th>
    <th>
      Does Nicotine Testing
    </th>
    <th colspan='3'>
      Actions
    </th>
  </tr>
  <tr>
    <td nowrap='nowrap'>
      Microsoft
    </td>
    <td>
      Yes
    </td>
    <td>
      No
    </td>
    <td nowrap='nowrap'>
      <a href="/employers/407">Show Portal</a>
    </td>
    <td>
      <a href="/employers/407/edit">Edit</a>
    </td>
    <td>
      <a href="/employers/407" onclick="if (confirm('Are you sure you want to delete the employer \'Microsoft\'? \n\nAll of this employer\'s locations will be deleted which include: \n  \nAll users associations to those locations will also be removed. \n\nThere is NO UNDO. Proceed?')) { var f = document.createElement('form'); f.style.display = 'none'; this.parentNode.appendChild(f); f.method = 'POST'; f.action = this.href;var m = document.createElement('input'); m.setAttribute('type', 'hidden'); m.setAttribute('name', '_method'); m.setAttribute('value', 'delete'); f.appendChild(m);f.submit(); };return false;">Delete</a>
    </td>
  </tr>
  <tr>
    <td nowrap='nowrap'>
      IBM
    </td>
    <td>
      Yes
    </td>
    <td>
      No
    </td>
    <td nowrap='nowrap'>
      <a href="/employers/406">Show Portal</a>
    </td>
    <td>
      <a href="/employers/406/edit">Edit</a>
    </td>
    <td>
      <a href="/employers/406" onclick="if (confirm('Are you sure you want to delete the employer \'IBM\'? \n\nAll of this employer\'s locations will be deleted which include: \n &nbsp;&nbsp;&nbsp;1) appleton\n \nAll users associations to those locations will also be removed. \n\nThere is NO UNDO. Proceed?')) { var f = document.createElement('form'); f.style.display = 'none'; this.parentNode.appendChild(f); f.method = 'POST'; f.action = this.href;var m = document.createElement('input'); m.setAttribute('type', 'hidden'); m.setAttribute('name', '_method'); m.setAttribute('value', 'delete'); f.appendChild(m);f.submit(); };return false;">Delete</a>
    </td>
  </tr>
</table>

## XPath Selector (not working) trying to target this link: <a href="/employers/406/edit">Edit</a> (the 2nd edit link in the table)

//*[.//text()='IBM' and .//a[text()='Edit']]

1 个答案:

答案 0 :(得分:1)

您的xpath正则表达式(“//* [.//td [conttains(。,'#{{{{{{{[(((((((((((((以数组的形式返回多个“范围”,因此您可以通过指定元素来指定要使用的范围:  范围[0] .click_link链接#为第一个  范围[1] .click_link链接#为第二个