【问题标题】:Selecting the Parent of the Parent element. Tables/Watir/Ruby选择父元素的父元素。表/Watir/Ruby
【发布时间】:2018-06-17 22:03:23
【问题描述】:

嘿,我想选择父元素的父元素。我有一个表,我试图在其中使用 Watir/Ruby 从同一行但在不同的列中选择编辑或删除按钮,例如。

名称 1 -> 编辑图标 -> 删除图标

名称 2 -> 编辑图标 -> 删除图标

示例代码如下:

<html>
 <body>
     <table class="table-responsive">
         <thead>...</thead>
             <tbody>
                 <tr>
                     <td class="col-name">Name 1</td>
                     <td> 
                         <a class="edit" href="/">
                             <span class="icon-edit"></span>
                         </a>
                         <a class="delete" href="/">
                             <span class="icon-delete"></span>
                         </a>
                     </td>
                 </tr>
                 <tr>
                     <td class="col-name">Name 2</td>
                     <td> 
                         <a class="edit" href="/">
                             <span class="icon-edit"></span>
                         </a>
                         <a class="delete" href="/">
                             <span class="icon-delete"></span>
                         </a>
                     </td>
                 </tr>
                 <tr>
                     ...
                 </tr>
         <tbody>
     </table>
 </body>
</html>

到目前为止,我已经尝试过了,但它不起作用。我收到一个错误 TypeError: can't convert Hash into an exact number

  table = @browser.table.when_present(:class => "table-responsive")
    iconrow = table.span(:text => "Name 1").parent
    if iconrow.a(:class => "edit").exists?
      iconrow.a(:class => "edit").click
    end

【问题讨论】:

  • 小心基于元素存在的条件,它们经常变得棘手。您的测试应该知道是否应该在开始时单击编辑按钮,并根据需要选择不带条件的正确方法。它使所需的 watir 代码更干净。

标签: ruby automated-tests watir


【解决方案1】:

代码可能存在几个问题: * :class 定位器应用于when_present 而不是表本身。 * “名称 1”的元素是 td 而不是 span

试试:

table = @browser.table(:class => "table-responsive").when_present
iconrow = table.td(:text => "Name 1").parent
if iconrow.a(:class => "edit").exists?
  iconrow.a(:class => "edit").click
end

【讨论】:

  • 我明白了,因为它在一个跨度中。谢谢
猜你喜欢
  • 2017-01-19
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-08-28
相关资源
最近更新 更多