2
votes

Does anyone know if one could do something like this in Watir?

1) Pick out a particular element from the source 2) Pick out all links under that element

I know it can be done in hpricot, but can it be done in Watir without hpricot?

Thanks

2

2 Answers

2
votes

To riff a bit on Alex's answer

links = browser.container(:how, what).links

links.each do |link|
  puts link.url
  puts link.text
end

container = some kind of container element such as: div, span, table, row, etc.

With tables it can get a bit trickier because often you want all the links in a given column, so then the code ends up looking like this

rows = browser.table(:how, what).rows  
  # .trs might work better than .rows if <th> are present

rows.each do |row|
  puts row.cell(:index, 2).link.url
  puts row.cell(:index, 2).link.text
end

(perhaps a better rubiest than myself can tell me if that could be done using this to dry things out a bit, or if it would not work?)

rows.each.cell(:index, 2).link do |link|
  puts link.url   
etc....
2
votes

Try:

@browser.div(:id,'mylinks').links