Search code examples
htmlrubywatirwatir-webdriver

Get specific nested table cell using Ruby Watir


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!


Solution

  • 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