How to fix Phantom.js not waiting for full page load with JavaScript?

Sometimes, we want to fix Phantom.js not waiting for full page load with JavaScript.

In this article, we’ll look at how to fix Phantom.js not waiting for full page load with JavaScript.

How to fix Phantom.js not waiting for full page load with JavaScript?

To fix Phantom.js not waiting for full page load with JavaScript, we can call page.render in the setTimeout callback.

For instance, we write

page.open(address, (status) => {
  if (status !== "success") {
    console.log("Unable to load the address!");
    phantom.exit();
  } else {
    window.setTimeout(() => {
      page.render(output);
      phantom.exit();
    }, 1000);
  }
});

to call page.open with the address URL string to open the page.

And then we call setTimeout with a callback that calls page.render to render the page.

We specify that we wait 1 second before rendering.

Conclusion

To fix Phantom.js not waiting for full page load with JavaScript, we can call page.render in the setTimeout callback.