0

I am scraping some HTML off a webpage and I'm trying to scrape off a table element off the page and convert it to JSON.

I found a node js library that does this but it requires a string as an argument. How do I turn the HTML object into a string? When I call the toString() function on it, it returns:

"[object HTMLTableElement]"

My code is :

let data = await page.evaluate(() => {
    componentTable = document.querySelector('table.xs-col-12');
    componentTable = componentTable.toString()
    return{
        componentTable
    }
})
console.log(data)

3 Answers3

3

To get all the HTML - including the element in question - as a string change:

componentTable.toString()

To:

componentTable.outerHTML
PeterKA
  • 24,158
  • 5
  • 26
  • 48
1

Just converting a HTMLElement to a string with toString() will give the object name, like you have seen.

You probably want to use innerHTML

return componentTable.innerHTML will give you a string of the html that is in that table, which depending on the node library you have found, may or may not be sufficient.

It's worth looking through the docs of that library, as they probably have an example for doing this.

Luke Storry
  • 6,032
  • 1
  • 9
  • 22
0

you want to use innerHtml, that is the key