I've done a lot of web scraping, where we have to deal with whatever
HTML the developer hacked together. Nested tables without id's or
name's can be a pain, but they can be dealt with. The methods may be
different depending on which version of Watir you're running though.
I'm using 1.5 from trunk here, and I recommend using one from trunk,
even though they're not officially "released" yet.
In these cases, IRB is your best friend. Here I've opened the browser
to table1.html in the unittests\html directory. I then start up IRB and
connect to that browser:
C:\Documents and Settings\davids>irb
irb(main):001:0> require 'watir'
=> true
irb(main):002:0> include Watir
=> Object
irb(main):003:0> ie = IE.attach(:title, /Test/)
[...]
Next, we look to see what tables we have:
irb(main):008:0> ie.show_tables
Found 6 tables
1 id= rows=2 columns=2
2 id=t1 rows=5 columns=1
3 id=t2 rows=2 columns=2
4 id= rows=1 columns=2
5 id=body_test rows=5 columns=1
6 id=pic_table rows=1 columns=4
=> nil
irb(main):009:0>
Here we'll look for the nested table example. Looks like table 3
matches the nested table on the screen. Note that table 4 is the table
*inside* table 3! We can confirm these guesses by showing the HTML for
each table:
irb(main):010:0> ie.table(:index, 3).html
=> "\r\n<TABLE id=t2 border=1><TBODY>\r\n<TR>\r\n<TD>cell 1
\r\n<TD>cell2 \r\n<T
R>\r\n<TD>\r\n<TABLE>\r\n<TBODY>\r\n<TR>\r\n<TD>nest1\r\n<TD>nest2</TR></TBODY><
/TABLE>\r\n<TD>Normal </TR></TBODY></TABLE>"
irb(main):011:0> ie.table(:index, 4).html
=>
"\r\n<TABLE><TBODY>\r\n<TR>\r\n<TD>nest1\r\n<TD>nest2</TR></TBODY></TABLE>"
irb(main):012:0>
Sure enough, you can see table 4 there in the middle of table three.
The reason this is important is because you can use indexes to find
cells in a table:
irb(main):013:0> ie.table(:index, 3)[1][1].text
=> "cell 1"
The trick is that the embedded tables are counted as part of the
containing table. This is why the row_count for table 3 shows *3* rows
instead of two:
irb(main):014:0> ie.table(:index, 3).row_count
=> 3
It's counting it's own two rows, plus the one row in the containing
table. Row 3 is the table 4's row 1, but though row_count reports 3
rows, it won't let you ACCESS row 3, because it's really in table 4!
irb(main):016:0> ie.table(:index, 3)[2][1].text
=> "nest1nest2"
irb(main):017:0> ie.table(:index, 3)[2][1].html
=>
"\r\n<TD><TABLE>\r\n<TBODY>\r\n<TR>\r\n<TD>nest1\r\n<TD>nest2</TR></TBODY></T
ABLE>"
irb(main):018:0> ie.table(:index, 4)[1][1].text
=> "nest1"
irb(main):019:0> ie.table(:index, 4)[1][2].text
=> "nest2"
irb(main):020:0> ie.table(:index, 3)[3][1].html
WIN32OLERuntimeError: Unknown property or method `2'
HRESULT error code:0x80020006
Unknown name.
from
c:/ruby/lib/ruby/gems/1.8/gems/watir-1.5.1.1077/./watir.rb:3193:in
`[]'
from
c:/ruby/lib/ruby/gems/1.8/gems/watir-1.5.1.1077/./watir.rb:3193:in
`row'
from
c:/ruby/lib/ruby/gems/1.8/gems/watir-1.5.1.1077/./watir.rb:3140:in
`[]'
from (irb):20
irb(main):021:0> ie.table(:index, 3)[3][1].text
WIN32OLERuntimeError: Unknown property or method `2'
HRESULT error code:0x80020006
Unknown name.
from
c:/ruby/lib/ruby/gems/1.8/gems/watir-1.5.1.1077/./watir.rb:3193:in
`[]'
from
c:/ruby/lib/ruby/gems/1.8/gems/watir-1.5.1.1077/./watir.rb:3193:in
`row'
from
c:/ruby/lib/ruby/gems/1.8/gems/watir-1.5.1.1077/./watir.rb:3140:in
`[]'
from (irb):21
irb(main):022:0>
That last row must be accessed via table 4.
There have been some discussions about fixing this inconsistency and
I've argued towards accuracy where row_count would only report the rows
in it's OWN table, and sub-tables would be accessed for their row count,
but others have been afraid of breaking existing tests which already
account for the inaccurate count.
Hope this helps you address the cells in your nested table situation.
David Schmidt
Vincent Predoehl wrote:
> I'm writing scripts to test a web site I didn't write myself. This
> web site has nested tables … not all of them have names … noobs.
>
> How do you access nested tables in watir? I've tried everything and
> am at my wit's end. I'm about ready to just grep the source code for
> the links, but I'd rather not do that.
>
> --
> Vincent
_______________________________________________
Wtr-general mailing list
[email protected]
http://rubyforge.org/mailman/listinfo/wtr-general