0

我有一个动态表,我想使用 watir 来选择一个编辑按钮。它没有我可以选择的传统 watir id(例如:browser.img(:title, "iconname")),并且可能有多个编辑图标可供选择。在过去,我会通过查询数据库来获得正确的元素。但是,这一次没有数据库条目来帮助我选择正确的编辑链接。

在下面的代码中,我尝试选择的内容来自显示“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>

I don't have trouble selecting a icon. Just trouble selecting the correct icon. Both the onclick and image values are selectable items. The icon may not be the last item in the list. I saw a post to try .last.click which does select the last icon in the list. Unfortunately the table posts the data in alphabetical order based on the Alias name. So it may not be the last item in the list and cannot use this method. Suggestions?

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

1 回答 1

1

It sounds like you need to find an element based on its siblings, so you might find this blog post useful. The post gives two options you might consider.

If the text you are looking for is unique - ie the row that has the text is definitely going to be the right row, you can find the td, go to the parent row and then get the link.

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

If you need to ensure that the text is in the first column, then you can do:

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

The tr.td.exists? is added since some of your rows do not have any tds, which would cause an exception when checking the second criteria.

于 2013-10-21T20:26:16.387 回答