Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6907089
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T08:23:32+00:00 2026-05-27T08:23:32+00:00

Basically all I would like to do is export a whole html table to

  • 0

Basically all I would like to do is export a whole html table to a .txt file (notepad document).

So far I have learnt how to instruct the browser to find the html page with the table.

require 'rubygems' 
require 'hpricot' 
require "watir-webdriver" 
url = "http://www.example.com"
browser = Watir::Browser.new 
browser.goto url

After running the above in cmd I can now see the html table in the browser.

This is where I am stuck. How do I use Watir to

  1. Find the tag
  2. collect everything (i.e. the html , and the text) which is within and .
  3. Extract those results to a .txt file (notepad document) and save it in a specific folder.

FYI the html table looks like this…

<table border="1" cellpadding="2">
<tr>
<th> Address </th>
<th> Council tax band </th>
<th> Annual council tax </th>
</tr>

<tr>
<td> 2, STONELEIGH AVENUE, COVENTRY, CV5 6BZ </td>
<td align="center"> F </td>
<td align="center"> &pound;2125 </td>
</tr>

……. The above row is repeated many time ……

</table> 

Then the table is closed.

So to re-cap my situation. I can use Watir to navigate the browser to the page containing the html table but my problem is that I am unsure of how to extract the results (everything within the tag – including the html) to a .txt file and then save that .txt file onto my computer.

I would prefer to take smaller steps with using Watir. I am knew to it therefore I would just like to learn how to extract the table and save everything that I have extracted into a .txt file. I have seen a couple of examples online using hpricot. However most of the examples seem to miss off code detailing how the array (if that is the correct approach) is outputted into a .txt file.

Could you help by demonstrating how to write a simple piece of code which will extract the html table ( and everything, including the , and everything in between) to a .txt notepad file?

Many thanks for your time.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-27T08:23:32+00:00Added an answer on May 27, 2026 at 8:23 am

    To get HTML of the entire table (if it is the only table on the page):

    browser.table.html
    

    You will get something like this:

    => "<table border=\"1\" cellpadding=\"2\">\n<tbody><tr>\n<th> Address </th>\n<th> Council tax band </th>\n<th> Annual council tax </th>\n</tr>\n\n<tr>\n<td> 2, STONELEIGH AVENUE, COVENTRY, CV5 6BZ </td>\n<td align=\"center\"> F </td>\n<td align=\"center\"> £2125 </td>\n</tr>\n\n</tbody></table>"
    

    To get HTML of each row and put it in an array:

    browser.table.trs.collect {|tr| tr.html}
    
    => ["<tr>\n<th> Address </th>\n<th> Council tax band </th>\n<th> Annual council tax </th>\n</tr>",
        "<tr>\n<td> 2, STONELEIGH AVENUE, COVENTRY, CV5 6BZ </td>\n<td align=\"center\"> F </td>\n<td align=\"center\"> £2125 </td>\n</tr>"] 
    

    To get text of each cell and put it in an array:

    browser.table.trs.collect {|tr| [tr[0].text, tr[1].text, tr[2].text]}
    => [["Address", "Council tax band", "Annual council tax"],
        ["2, STONELEIGH AVENUE, COVENTRY, CV5 6BZ", "F", "£2125"]] 
    

    To write text of each cell to file:

    content = b.table.trs.collect {|tr| [tr[0].text, tr[1].text, tr[2].text]}
    File.open("table.txt", "w") {|file| file.puts content}
    

    The file will look like this:

    Address
    Council tax band
    Annual council tax
    2, STONELEIGH AVENUE, COVENTRY, CV5 6BZ
    F
    £2125
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Basically, I would like to decode a given HTML document, and replace all special
Basically I would like to have all databases in a certain instance have the
I would like to have a script with an infinite loop, that kills all
I would now like to make an app that works on basically all html5
I have a number of stored procs which I would like to all run
I basically have a square image depicting a diagram and what I would like
I have some code that is basically 5 print statements that i would like
I'd like to implement mod_rewrite to put my site into maintenance. Basically all IP
I'm testing a C++ class with a number of functions that all have basically
I'm a little bit stuck with the following problem: I would like all my

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.