根据单元格内容选择表格行中的链接

时间:2012-04-03 12:42:14

标签: xpath selenium-ide

使用Selenium IDE,我试图在表格行中找到一个链接。表行是动态生成的。每行都有一些带有文本内容的单元格和一个可能有两个或更多链接的单元格。

我想要做的是创建一个自动化测试,点击特定文本引用的特定行中的特定链接。例如,在下表中:

<html>
<body>
<table border="1">
 <tr>
   <td>
    Hello World!
   </td>
   <td>
    Type Greeting
   </td>
   <td>
    <a href="www.foo.com/view/id=123456">View</a>
    <a href="www.foo.com/delete/id=123456">Delete</a>
   </td>
 </tr>
 <tr>
   <td>
    Buenos Dias!
   </td>
   <td>
    Type Greeting 
   </td>
   <td>
    <a href="www.foo.com/view/id=789101">View</a>
    <a href="www.foo.com/delete/id=789101">Delete</a>
   </td>
 </tr>
</table>
</body>
</html>

情景是,在上一页我添加数据“Buenos Dias!”。下一页将显示添加的所有数据的表格。我希望能够点击刚刚添加的数据的“查看”链接(在本例中为“Buenos Dias!”条目)。

通过引用文本“Buenos Dias!”找到“查看”链接的好方法是什么?

我能够找到包含文本的单元格:

//td[text()="Buenos Dias!"] 

但是我想以某种方式使用它作为参考,从表中找到它各自的“视图”链接。我尝试了兄弟格式(以下,前面),我似乎无法让它正常工作。使用绝对xpath(html / body / table / tbody / tr [1] / td [3] / a [1])将不起作用,因为数据可能会更改并且条目显示在不同的行上。

提前致谢,如果您需要更多信息,请与我们联系。

  • Selenium IDE 1.7.2
  • Firefox 9.0.1
  • Mac OS X Lion 10.7.3

3 个答案:

答案 0 :(得分:4)

使用

(//td[normalize-space() ="Buenos Dias!"])[1]/following-sibling::td[2]/a[. = 'View']

答案 1 :(得分:0)

//find table first
 WebElement q=  d.findElement(By.xpath("//*[@id='applicationcontent']/div[6]/table/tbody/tr"));

// find a unique value of the row and click on link of the row which is after the text you find.
         q.findElement(By.xpath("//td[contains(text(),'abcd@gmail.com')]/following-sibling::td/a[@id='edit.png']")).click();

答案 2 :(得分:0)

(// td [normalize-space()=“Buenos Dias!”])[1] / follow-sibling :: td [2] / a [。 ='查看']