单击带有 xpath 和 cucumber 的表中的编辑链接

发布于 2024-08-31 23:59:17 字数 3239 浏览 1 评论 0原文

如何使用 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']]

How can I use cucumber to click an edit link in a list of items in a table using xpath?

I've got a selector working that returns the link I need, but it's one of many results.... how can I return only 1 result?

Here is my cucumber step

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

As you can see this will work, but will just click the first edit link in the table even though it knows about the others that it has matched... so that's no good.

Here is the table and selector: http://pastie.textmate.org/private/9od335pmsj4hi9nbe9lbow

Thank you!

This is the source of that file as well in case the service is down or linking to external code is frowned upon.

## 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    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']]

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

不如归去 2024-09-07 23:59:17

您的 xpath 正则表达式 ("//*[.//td[contains(.,'#{cell_value}')] 和 .//a[text()='#{link}']]") 返回超过一个数组形式的“范围”,因此您可以通过指定元素来指定要使用的范围:
第一个的范围[0].click_link 链接#
第二个的范围[1].click_link链接#

Your xpath regex ("//*[.//td[contains(.,'#{cell_value}')] and .//a[text()='#{link}']]") is returning more than one "scope" in the form of an array, so you can specify which one to use by specifying the element:
scope[0].click_link link # for the first
scope[1].click_link link # for the second

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文