Search code examples
rubyselenium-webdriverwatirwatir-webdriverpage-object-gem

how to click a link in a table based on the text in a row


Using page-object and watir-webdriver how can I click a link in a table, based on the row text as below:

The table contains 3 rows which have names in the first column, and a corresponding Details link in columns to the right:

DASHBOARD .... Details

EXAMPLE .... Details

and so on.

<div class="basicGridHeader">
    <table class="basicGridTable">ALL THE DETAILS: 
    ....soon
    </table>

</div>

<div class="basicGridWrapper">
    <table class="basicGridTable">
        <tbody id="bicFac9" class="ide043">
            <tr id="id056">
                <td class="bicRowFac10">
                    <span>
                        <label class="bicDeco5">
                            <b>DASHBOARD:</b> ---> Based on this text
                        </label>
                    </span>
                </td>

                <td class="bicRowFac11">
                    ....some element
                </td>

                <td class="bicRowFac12">
                    <span>
                        <a class="bicFacDet5">Details</a> ---> I should able click this link
                    </span>
                </td>
            </tr>
        </tbody>
    </table>
</div>

Solution

  • You could locate a cell that contains the specified text, go to the parent row and then find the details link in that row.

    Assuming that there might be other detail links you would want to click, I would define a view_details method that accepts the text of the row you want to locate:

    class MyPage
      include PageObject
    
      table(:grid){ div_element(:class => 'basicGridWrapper')
        .table_element(:class => 'basicGridTable') }
    
      def view_details(label)
        grid_element.cell_element(:text => /#{label}/)
          .parent
          .link_element(:text => 'Details')
          .click
      end
    
    end
    

    You can then click the link with:

    page.view_details('DASHBOARD')