Watir: How to access a table without an ID or NAME

4.6k views Asked by At

I am trying to write my watir script to grab the following data (the table body headers and the table row data, but I am having trouble trying to figure out how to access the table. (Once I get that, teh rest is a piece of cake).

Can anyone come up with something that will help me access the table? It doesn't have a name or an ID...

<div id="income">
    <table class="tHe" cellspacing="0">
    <thead>
        <tr>
            <th id="companyLabel" class="tFirst" style="width:30%"> Quarter Ending  </th>
            <th id="201004" class="tFirst right">Apr&nbsp;10 </th>
            <th id="201001" class="tFirst right">Jan&nbsp;10 </th>
            <th id="200910" class="tFirst right">Oct&nbsp;09 </th>
            <th id="200907" class="tFirst right">Jul&nbsp;09 </th>
            <th id="200904" class="tFirst right">Apr&nbsp;09 </th>
        </tr>
    </thead>

    <tbody id="revenueBody">
    <tr>
        <td  class="indtr">Totals</dfn></td>
        <td class="right">2849.00</td>
        <td class="right">3177.00</td>
        <td class="right">5950.00</td>
        <td class="right">4451.00</td>
        <td class="right">3351.00</td>
    </tr>
    ...
3

There are 3 answers

1
marc On BEST ANSWER

ie.table(:class=>'tHe') should work if there's no other tables with the same class name

ie.table(:after?, ie.div(:id, 'income')) should work if there's no other div with id 'income'

or ie.table(:index=>0) - you would need to check your page to see what the correct index value for your table is.

2
Željko Filipin On

But wait, there is more! :)

browser.div(:id => "income").table(:class => 'tHe')
browser.div(:id => "income").table(:index => 1)
...
0
Richard Conroy On

There is also XPath if you are stuck.

If you fire up the page and access it through Firebug or your browser's native developer tools, you can find the xpath expression for the table and then plug that into the Watir API call.

I think it was in later versions of Watir 1.5.x that support for advanced page querying came in (basically your problem, where there are no ID tags). This page on the watir wiki should help: Ways Available To Identify HTML Element