I am new to PhantomJS and trying to capture the homepage of Trade Me. Here's my code so far:
var page = require('webpage').create();
page.open('http://trademe.co.nz', function () {
// Checks for bottom div and scrolls down from time to time
window.setInterval(function() {
// Checks if there is a div with class=".has-more-items"
// (not sure if this is the best way of doing it)
// var count = page.content.match(/class=".site-footer"/g);
var footer_visible = page.evaluate(function() {
return $('.site-footer').is(':visible');
});
if(!footer_visible) { // Didn't find
console.log('Scrolling');
page.evaluate(function() {
// Scrolls to the bottom of page
window.document.body.scrollTop = document.body.scrollHeight;
});
}
else { // Found
console.log('Found');
// Do what you want
window.setTimeout( function() {
console.log('Capturing');
page.render('phantom-capture.png', {format: 'png'});
phantom.exit();
}, 10000);
}
}, 1000); // Number of milliseconds to wait between scrolls
});
There are several things that baffle me:
- The word
Scrolling
never gets printed. - It eventually gets to
Found
, and the word is printed 10 times. I assume that's because it is contained within thesetInterval
block with a 1 second interval, and there's a 10 second wait caused by thesetTimeout
? - The page is finally rendered to the PNG file, but the content of those asynchronously loaded panels are still empty, and showing the
Loading...
message.
I'm new to all this and my knowledge of Javascript is very rusty.