I'm using PhantomJS v1.4.1 to load some web pages. I don't have access to their server-side, I just getting links pointing to them. I'm using obsolete version of Phantom because I need to support Adobe Flash on that web pages.
The problem is many web-sites are loading their minor content async and that's why Phantom's onLoadFinished callback (analogue for onLoad in HTML) fired too early when not everything still has loaded. Can anyone suggest how can I wait for full load of a webpage to make, for example, a screenshot with all dynamic content like ads?
This is an implementation of Supr's answer. Also it uses setTimeout instead of setInterval as Mateusz Charytoniuk suggested.
Phantomjs will exit in 1000ms when there isn't any request or response.
I use a personnal blend of the phantomjs
waitfor.js
example.This is my
main.js
file:And the
lib/waitFor.js
file (which is just a copy and paste of thewaifFor()
function from the phantomjswaitfor.js
example):This method is not asynchronous but at least am I assured that all the resources were loaded before I try using them.
I found this solution useful in a NodeJS app. I use it just in desperate cases because it launches a timeout in order to wait for the full page load.
The second argument is the callback function which is going to be called once the response is ready.
Maybe you can use the
onResourceRequested
andonResourceReceived
callbacks to detect asynchronous loading. Here's an example of using those callbacks from their documentation:Also, you can look at
examples/netsniff.js
for a working example.This the code I use:
Basically given the fact you're supposed to know that the page is full downloaded when a given element appears on the DOM. So the script is going to wait until this happens.
Another approach is to just ask PhantomJS to wait for a bit after the page has loaded before doing the render, as per the regular rasterize.js example, but with a longer timeout to allow the JavaScript to finish loading additional resources: