2013-10-21 29 views
0

我有一個dynnamic表格,我想用watir來選擇一個編輯按鈕。它沒有我可以選擇的傳統watir ID(例如:browser.img(:title,「iconname」)),並且可能有多個編輯圖標可供選擇。在過去,我會通過查詢數據庫來獲得正確的元素。但是,這次沒有數據庫條目來幫助我選擇正確的編輯鏈接。使用watir單擊沒有標識的表格元素

在下面的代碼中,我試圖選擇是從它顯示「autogenerated3」部分我試圖選擇「onclick」元素或「img src」 這兩個都是可選項目,將單擊編輯圖標。

<div id="certificate_table" style="margin-bottom: 1em"> 
<table cellspacing="0"> 
<tbody> 
<tr> 
<th>Alias</th> 
<th>Common Name</th> 
<th class="centered">Status</th> 
<th class="centered">In Use</th> 
<th class="centered">Issued</th> 
<th class="centered">Expires</th> 
<th class="centered">Days</th> 
<th>Actions</th> 
</tr> 
<tr class="normal"> 
<td>autogenerated2</td> 
<td>default.domain.com</td> 
<td class="centered"> Revoked </td> 
<td class="centered"> 
<td class="centered"> 10/18/2013 19:46:34 GMT </td> 
<td class="centered"> 10/17/2016 19:46:34 GMT </td> 
<td class="centered">N/A</td> 
<td> 
<a onclick="new Ajax.Request('/media/certificates/edit_certificate/3',  {asynchronous:true, evalScripts:true}); return false;" href="#"> 
<img title="Edit" src="/media/images/icons/edit.gif?1276876449" alt="Edit"> 
</a> 
</td> 
</tr> 
<tr class="alt"> 
<td>autogenerated3</td> 
<td>autogenerated3.domain.com</td> 
<td class="centered"> CSR Issued </td> 
<td class="centered"> 
<td class="centered"> 10/18/2013 20:54:55 GMT </td> 
<td class="centered"> 10/17/2016 20:54:55 GMT </td> 
<td class="centered">1092 </td> 
<td> 
<a onclick="new Ajax.Request('/media/certificates/edit_certificate/4', {asynchronous:true, evalScripts:true}); return false;" href="#"> 
<img title="Edit" src="/media/images/icons/edit.gif?1276876449" alt="Edit"> 
</a> 
<a onclick="new Ajax.Request('/media/certificates/generate_csr/4', {asynchronous:true, evalScripts:true}); return false;" href="#"> 
<a onclick="new Ajax.Request('/media/certificates/import_certificate_reply/4', {asynchronous:true, evalScripts:true}); return false;" href="#"> 
<a onclick="if (confirm('Are you sure you want to revoke this certificate?')) { new Ajax.Request('/media/certificates/revoke_certificate/4', {asynchronous:true, evalScripts:true}); }; return false;" href="#"> 
</td> 
</tr> 
<tr class="normal"> 
<td>Original Certificate</td> 
<td>localhost.localdomain</td> 
<td class="centered"> Self Signed </td> 
<td class="centered"> 
<td class="centered"> 10/03/2013 22:37:02 GMT </td> 
<td class="centered"> 10/03/2014 22:37:02 GMT </td> 
<td class="centered">347 </td> 
<td> 
    <a onclick="new Ajax.Request('/media/certificates/edit_certificate/1', {asynchronous:true, evalScripts:true}); return false;" href="#"> 
    <img title="Edit" src="/media/images/icons/edit.gif?1276876449" alt="Edit"> 
    </a> 
</td> 
</tr> 
    <tr class="alt"> 
    <td>vhost4</td> 
    <td>vhost4.domain.com</td> 
    <td class="centered"> Revoked </td> 
    <td class="centered"> 
    <td class="centered"> 10/18/2013 15:58:01 GMT </td> 
    <td class="centered"> 10/17/2016 15:58:01 GMT </td> 
    <td class="centered">N/A</td> 
    <td> 
    <a onclick="new Ajax.Request('/media/certificates/edit_certificate/2', {asynchronous:true, evalScripts:true}); return false;" href="#"> 
    <img title="Edit" src="/media/images/icons/edit.gif?1276876449" alt="Edit"> 
    </a> 
    </td> 
    </tr> 
</tbody> 
</table> 

我沒有麻煩選擇一個圖標。只是麻煩選擇正確的圖標。 onclick和圖像值都是可選項目。該圖標可能不是列表中的最後一項。我看到一個帖子試圖.last.click哪個選擇列表中的最後一個圖標。不幸的是,表格根據別名按字母順序發佈數據。所以它可能不是列表中的最後一項,並且不能使用此方法。建議?

b.div(:id, "certificate_table").imgs(:src => "/media/images/icons/edit.gif?1276876449").last.when_present.click 

回答

1

這聽起來像你需要找到基於其兄弟姐妹的元素,所以你可能會發現這個blog post有用。這篇文章給出了你可能考慮的兩個選項。

如果您正在尋找的文本是唯一的 - 即具有文本的行肯定會是正確的行,您可以找到td,轉到父行然後獲取鏈接。

b.td(:text => 'autogenerated3').parent.link.click 

如果您需要確保該文本是在第一列,那麼你可以做:

b.trs.find{ |tr| 
    tr.td.exists? and tr.td(:index => 0).text == 'autogenerated3' 
}.link.click 

tr.td.exists?,因爲你的一些行的加入沒有任何TDS,這將檢查第二個標準時會導致異常。

+0

爲什麼不使用b.td(索引:0,文本:'autogenerated3')? – p0deje

+0

@ p0deje,你的意思是第二個例子嗎?我不認爲這是相同的。 'b.td(索引:0,文本:'autogenerated3')'將是第一個td元素(在任何列中)具有文本。我試圖確保文本位於該行的第一列。 –

+0

謝謝 b.td(:text =>'autogenerated3')。parent.link.click很棒! –

相關問題