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

3

5

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>
    ...
Spivey answered 8/8, 2010 at 14:30 Comment(0)
A
7

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.

Assam answered 8/8, 2010 at 17:50 Comment(1)
can we find the index of table on the base of its id?Jonis
F
6

But wait, there is more! :)

browser.div(:id => "income").table(:class => 'tHe')
browser.div(:id => "income").table(:index => 1)
...
Forage answered 9/8, 2010 at 9:40 Comment(2)
Thanks for this... I am actually going to be able to use this for the same project, just in a different area...Spivey
just one small detail: add ":" before id. Example: browser.div(:id => "income")Delanadelancey
V
4

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

Vanwinkle answered 9/8, 2010 at 10:15 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.