Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Return array of hrefs

I need Watir to return an array of hrefs. These hrefs are all in the same div and they all have the same class. My code to grab the first href on the page looks like this:

b.div(:class => 'products').link(:class => 'product_detail_url').href

But, after that I can't find out how to loop through all available hrefs in that div.

I honestly looked all over the place, but none of the solutions I found seemed to address my specific need. Thanks for reading, hopefully someone has a solution.

like image 870
jonsie_araki Avatar asked Jan 21 '26 07:01

jonsie_araki


1 Answers

Try:

b.div(:class => 'products').links(:class => 'product_detail_url').each do |link|
  puts link.href
end

Basically this iterates through a collection of links that have the specific class.

Update:

To collect all of the hrefs into an array, use the collect method:

link_arr = b.div(:class => 'products').links(:class => 'product_detail_url').collect{ |link| link.href }

Or break it into two lines for easier reading:

link_collection = b.div(:class => 'products').links(:class => 'product_detail_url')
link_arr = link_collection.collect{ |link| link.href }
like image 149
Justin Ko Avatar answered Jan 23 '26 21:01

Justin Ko



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!