这是我的phantomjs超级简单代码(test2.js):
var page = require('webpage').create();
page.open('https://instagram.com', function(status) {
console.log("Status: " + status);
if(status === "success") {
page.render('example.png');
}
phantom.exit();
});
cmd代码:
phantomjs.exe --ignore-ssl-errors=true test2.js
结果-屏幕截图始终为黑色,空白
答案 0 :(得分:1)
问题是,截屏时尚未处理该页面。您应该等待页面完全加载(使用javascript处理)。您可以通过更改以下行来简单地检查一下:
page.render('example.png');
像这样:
window.setTimeout(function(){
page.render('example.png');
phantom.exit();
},15000);
请注意,等待一定的时间并不是一个好主意...最好使用一些WaitFor
函数...
例如:
function waitFor(testFx, onReady, timeOutMillis) {
var maxtimeOutMillis = timeOutMillis ? timeOutMillis : 5000, //< Default Max Timout is 5s
start = new Date().getTime(),
condition = false,
interval = setInterval(function() {
if ( (new Date().getTime() - start < maxtimeOutMillis) && !condition ) {
// If not time-out yet and condition not yet fulfilled
condition = (typeof(testFx) === "string" ? eval(testFx) : testFx()); //< defensive code
} else {
if(!condition) {
// If condition still not fulfilled (timeout but condition is 'false')
console.log(" * Timeout, exiting.");
phantom.exit(1);
} else {
// Condition fulfilled (timeout and/or condition is 'true')
typeof(onReady) === "string" ? eval(onReady) : onReady(); //< Do what it's supposed to do once the condition is fulfilled
clearInterval(interval); //< Stop this interval
}
}
}, 250); //< repeat check every 250ms
};
用法示例:
waitFor(function(){
page.evaluate(function(){
return ((document.documentElement.textContent || document.documentElement.innerText).indexOf('© 2018 INSTAGRAM') > -1); // <-- this may be some other string or other condition - don't know instagram site at all... generally it should return true if the element was found and false if not.
});
},function(){
page.render('example.png');
}
);