Get specific nested table cell using Ruby Watir

1.2k views Asked by At

I'm writing a Watir program to extract a specific number from a dynamically-generated table. The number is always in the second column of the fourth-to-last row in the table, but I'm getting the following error when trying to access the general table element:

Watir::Table: located: false; {:id=>"tContenido", :tag_name=>"table"}

Code

require 'rubygems'
require 'watir'
browser = Watir::Browser.new :firefox
$log = Logger.new('logs\test.log')
$log.info("TEST")
$log.info browser.table(:id, 'tContenido')

HTML Structure of the target page (1) HTML Structure of the target page (1)

HTML Structure of the target page (2) HTML Structure of the target page (2)

Any help would be greatly appreciated, thanks in advance!

2

There are 2 answers

4
titusfortner On BEST ANSWER

I think this is the one you are looking for:

browser.table(id: 'tContenido').tr(class: 'tr_gris', index -1).td(index: 1).text

Alternately, if you are having problems with a table, you can just put everything into an array of Hashes:

browser.table(id: 'tContenido').hashes
1
Rajagopalan On

You can write the following WATIR code

puts b.table(id: 'tContenido').trs[-4].tds[2].text