简体   繁体   中英

watir-webdriver iterating table for field comparison

How can I iterate this table's rows and compare the value of each first column, ie 'Wallet1' == 'Wallet2' ?

And when the field has the same value, compare the 4th column value?

<table class="simple">
    <tbody><tr>
        <th class="labelCenter">Saldo</th>

        (...)

    </tr>
        <tr class="odd">
            <td class="labelCenter">Wallet 1</td>
            <td class="labelCenter">Decresing</td>
            <td class="labelCenter">16/02/2012 19:06:01</td>
            <td class="labelCenter">19/02/2012 14:03:01</td>
                <td class="labelCenter">
                </td>       
            <td class="labelRight">
                    78,90
            </td>
                <td class="labelRight">
                    0,00
                </td>
            <td class="labelCenter">Value</td>
        </tr>
        <tr class="even">
            <td class="labelCenter">Wallet 2</td>
        <td class="labelCenter">Increasing</td>
        <td class="labelCenter">16/02/2012 19:06:01</td>
        <td class="labelCenter">19/02/2012 11:09:01</td>    
                <td class="labelCenter">
                </td>
        <td class="labelRight">
                    0,00
        </td>
            <td class="labelRight">
                0,00
            </td>
        <td class="labelCenter">Value</td>
    </tr>
</tbody></table>    

My first approach used variations of,

$browser.table(:class, 'simple').rows[1..-1].each do |row|

but I'm facing a roadblock.

Also, why does not this work?

$browser.tr(:class => /odd|even/) do |row|
  puts row(:index => 0).text

This could probably be made faster by sorting the collection first or something, or maybe extracting all the values into arrays and using some array functions to find the matching rows. I'm sure there is room to make this more elegant

start with the first row, compare to every row below it, move to next row, repeat

rows = $browser.table(:class, 'simple').rows
last = rows.length -1
last.times do |current|
  remaining = last - current
  remaining.times do |j|
    if rows[current].cell.text == rows[j+1].cell.text
      if rows[current].cell(:index => 3).text == rows[j+1].cell(:index => 3).text
        #do something
      end
    end
  end
end
$browser.table.trs(:class => /odd|even/).each do |tr|
  puts tr.td(:index => 0).text
end

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM