Search code examples
rubyautomated-testswatir

Selecting the Parent of the Parent element. Tables/Watir/Ruby


Hey i am trying to select the parent of the parent element i suppose. I have a table in which i am trying to use Watir/Ruby to select the edit or delete button from the same row but in a different coloumn, example.

Name 1 -> Edit Icon -> Delete Icon

Name 2 -> Edit Icon -> Delete Icon

Example code below:

<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>

So far i have tried this but it is not working. I get an error 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

Solution

  • There might be a couple issues with the code: * The :class locator is being applied to when_present rather than the table itself. * The element with "Name 1" is a td rather than a span.

    Try:

    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