2010-05-10 73 views
1

如何使用黄瓜单击xpath中表格中的项目列表中的编辑链接?点击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']] 
+0

什么是XML文档,究竟需要选择什么?请在您的问题中提供足够的信息。 – 2010-05-10 20:28:44

+0

Dimitre,你看过我在那里的pastie链接吗?它具有完整的

列在那里与我正在使用的当前xpath选择器。如果你看看它,你可以看到我正在尝试选择中包含文本“IBM”的“编辑”链接。如果我的问题仍不清楚,请告诉我。谢谢! – jondkinney2010-05-10 20:34:13

+0

这里有一个相关的问题/答案: http://stackoverflow.com/questions/4248889/cucumber-selecting-an-element-from-a-table-for-deletion-or-addition/7618578# 7618578 – mongo296 2011-10-01 06:32:54

回答

1

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