Reputation: 353
I am writing text content from a div element to a csv file using the ruby csv gem. When I output this div's text content to the console, I get all the text content including that from child divs. When I write to the csv file, it only outputs the text content from 'a' child elements. I need it to also include all the text from a child div and it's 'a' elements. Here is the HTML:
<div class="bodytag" style="padding-bottom:30px; overflow:visible">
<h2>Search Results for "serialnum3"</h2>
<div id="results_banner">
Products
<span>Showing 1 to 2 of 2 results</span>
</div>
<div class="pg_dir"></div>
<div class="results_row">
<a href="/products/fuji/" title="Fuji, Inc.">FUJI</a>
<a href="/products/fuji/50mm lens/" title="Fuji, Inc.">50mm lens</a>
<div class="results_subrow">
<p>more product info</p>
</div>
</div>
<div class="results_row">
<a href="/products/fuji/" title="Fuji, Inc. 2">FUJI</a>
<a href="/products/fuji/50mm lens/" title="Fuji, Inc.">50mm lens</a>
<div class="results_subrow">
<p>more product info 2</p>
</div>
Here is my code:
if
browser.div(:class => "results_row").exists?
search_results = browser.divs(class: "results_row").map(&:text)
csv << search_results
else
csv << %w("no search results")
end
My end goal is to use the csv data as an excel column so if anyone knows an easier way I'd love to hear it! Right now I am successfully using roo to parse an excel column as an array for search terms. I would like to use roo to write the div element's text content in an adjacent column instead of writing to csv but couldn't figure out how to do it with roo.
Upvotes: 0
Views: 300