使用 watir 单击没有 id 的表格元素 [英] Using watir to click table element without id

查看:19
本文介绍了使用 watir 单击没有 id 的表格元素的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

在下面的代码中,我试图选择的是显示autogenerated3"的部分,我试图选择onclick"元素或img src"两者都是可以单击编辑图标的可选项目.

<table cellpacing="0"><tr><th>别名</th><th>通用名称</th><th class="centered">状态</th><th class="centered">使用中</th><th class="center">已发布</th><th class="central">过期</th><th class="center化">Days</th><th>动作</th></tr><tr class="普通"><td>自动生成2</td><td>default.domain.com</td><td class="居中">撤销 </td><td class="居中"><td class="居中">10/18/2013 19:46:34 GMT </td><td class="居中">10/17/2016 19:46:34 GMT </td><td class="center">N/A</td><td><a onclick="new Ajax.Request('/media/certificates/edit_certificate/3', {asynchronous:true, evalScripts:true}); return false;"href="#"><img title="编辑" src="/media/images/icons/edit.gif?1276876449" alt="编辑"></a></td></tr><td>自动生成3</td><td>autogenerated3.domain.com</td><td class="居中">CSR 发布 </td><td class="居中"><td class="居中">2013 年 10 月 18 日 20:54:55 GMT </td><td class="居中">10/17/2016 20:54:55 GMT </td><td class="center">1092 </td><td><a onclick="new Ajax.Request('/media/certificates/edit_certificate/4', {asynchronous:true, evalScripts:true}); return false;"href="#"><img title="编辑" src="/media/images/icons/edit.gif?1276876449" alt="编辑"></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}); }; 返回假;"href="#"></td></tr><tr class="普通"><td>原始证书</td><td>localhost.localdomain</td><td class="居中">自签名</td><td class="居中"><td class="居中">10/03/2013 22:37:02 GMT </td><td class="居中">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="编辑" src="/media/images/icons/edit.gif?1276876449" alt="编辑"></a></td></tr><td>vhost4</td><td>vhost4.domain.com</td><td class="居中">撤销 </td><td class="居中"><td class="居中">10/18/2013 15:58:01 GMT </td><td class="居中">10/17/2016 15:58:01 GMT </td><td class="center">N/A</td><td><a onclick="new Ajax.Request('/media/certificates/edit_certificate/2', {asynchronous:true, evalScripts:true}); return false;"href="#"><img title="编辑" src="/media/images/icons/edit.gif?1276876449" alt="编辑"></a></td></tr></tbody>

我选择图标没有问题.只是麻烦选择了正确的图标.onclick 和 image 值都是可选项目.该图标可能不是列表中的最后一项.我看到了一篇尝试 .last.click 的帖子,它确实选择了列表中的最后一个图标.不幸的是,该表根据别名按字母顺序发布数据.所以它可能不是列表中的最后一项,不能使用这种方法.建议?

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

解决方案

听起来你需要根据它的兄弟元素找到一个元素,所以你可能会找到这个 博文 很有用.该帖子提供了两个您可能会考虑的选项.

如果您要查找的文本是唯一的 - 即包含该文本的行肯定是正确的行,您可以找到 td,转到父行,然后获取链接.

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

如果你需要确保文本在第一列,那么你可以这样做:

b.trs.find{ |tr|tr.td.存在吗?和 tr.td(:index => 0).text == 'autogenerated3'}.link.click

添加 tr.td.exists? 是因为您的某些行没有任何 tds,这会在检查第二个条件时导致异常.

I have a dynnamic table that I would like to use watir to select a edit button. It doesn't have a traditional watir id that I can select (example: browser.img(:title, "iconname")) and there may be multiple edit icons to choose from. In the past I would get the correct element by querying the database. However this does not have a database entry to help me select the correct edit link this time.

In the code below what I am trying to select is from the section where it shows "autogenerated3" I am trying to select either the "onclick" element or the "img src" Both are selectable items that will click the edit icon.

<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

解决方案

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.

这篇关于使用 watir 单击没有 id 的表格元素的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆