2012-04-03 48 views
1

使用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!」找到「視圖」鏈接的好方法是什麼?

我能夠找到其中包含文本的單元格:

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

但我想以某種方式使用它作爲從表中定位各自的「查看」鏈接的參考。我嘗試了兄弟格式(下面,之前),我似乎無法讓它正常工作。使用絕對xpath(html/body/table/tbody/tr [1]/td [3]/a [1])將無法正常工作,因爲數據可能會更改並且條目出現在不同的行上。

在此先感謝您,並告訴我您是否需要更多信息。

  • 硒IDE 1.7.2
  • 火狐9.0.1
  • 的Mac OS X獅子10.7.3

回答

3

使用

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

謝謝Dimitre!這對我有效。 – RaymundS 2012-04-03 16:18:59

+0

@RaymundS:不客氣 – 2012-04-03 16:21:33

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(),'[email protected]')]/following-sibling::td/a[@id='edit.png']")).click(); 
0

(//td [normalize-space()=「Buenos Dias!」])[1]/following-sibling :: td [ 2]/A [。 ='查看']

相關問題