[SOLVED] Waiting for an HTML page to be fully rendered, then doing DOM manipulation


This Content is from Stack Overflow. Question asked by Asker

I’m trying to get a specific element from the DOM of a webpage using nodeJs. To do so, I used jsdom and everything seems to be perfect with regular webpages and I am able to see the DOM in nodejs and get and select elements.

The problem is on some webpages like this one, when you go to the page there is an initial webpage and then after fetching new data, the page changes and gets updated. This is when my desired DOM element appears. My code shows the initial DOM structure of the website so I am not able to get that specific dom, since it will be added after like 5 seconds to the webpage.

How can I wait for the website to be fully rendered and get updated and then get its DOM?

Here is my code:

const jsdom = require('jsdom');
const { JSDOM } = jsdom;

const url =

JSDOM.fromURL(url).then((dom) => {
      .forEach((e) => console.log(e.innerHTML))


You need a headless browser. There are simple scraping tools like jsdom or cheerio in NodeJS, but they’re not able to run JS after the dom loads. Modern browsers can, and that’s why you’re not able to do what you want. Back when I wanted to convert fully-rendered AngularJS pages into PDFs, we had to use PhantomJS.

Nowadays, that project has fallen out of favor since we have Headless Chrome. But now, it appears that someone has written an even-more-simplified wrapper around it, called Puppeteer (credit: GrafiCode).

The other option, which has been around for a long time and is very powerful, is Selenium. This one is good if you want to know how your UI code runs/looks in different browsers. That’s probably a bit much for what you’re trying to do, but I figured I’d give you the info I had on the topic.

This Question was asked in StackOverflow by Asker and Answered by danieldaugherty It is licensed under the terms of CC BY-SA 2.5. - CC BY-SA 3.0. - CC BY-SA 4.0.

people found this article helpful. What about you?