Javascript Can';t拿instagram.com上的Phantomjs代码截图为例,为什么';总是黑屏吗?
下面是我的phantomjs超级简单代码(test2.js): cmd代码:Javascript Can';t拿instagram.com上的Phantomjs代码截图为例,为什么';总是黑屏吗?,javascript,selenium,phantomjs,casperjs,Javascript,Selenium,Phantomjs,Casperjs,下面是我的phantomjs超级简单代码(test2.js): cmd代码: phantomjs.exe --ignore-ssl-errors=true test2.js 结果-始终为黑色屏幕截图,空白问题是,当您拍摄屏幕截图时,该页面尚未处理。您应该等待页面完全加载(使用javascript处理)。您只需更改此行即可检查: page.render('example.png'); 对这样的事情: window.setTimeout(function(){ page.render('
phantomjs.exe --ignore-ssl-errors=true test2.js
结果-始终为黑色屏幕截图,空白问题是,当您拍摄屏幕截图时,该页面尚未处理。您应该等待页面完全加载(使用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(testFx、onReady、timeOutMillis){
var maxtimeOutMillis=timeOutMillis?timeOutMillis:5000,//<默认最大超时为5s
开始=新日期().getTime(),
条件=假,
间隔=设置间隔(函数(){
如果((新日期().getTime()-start
用法示例:
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');
}
);
waitFor(函数(){
page.evaluate(函数(){
return((document.documentElement.textContent | | document.documentElement.innerText).indexOf('©2018 INSTAGRAM')>-1);//它可能在完全加载屏幕截图之前捕获屏幕截图,设置一个条件以检查元素是否已加载,然后调用渲染。另外:您应该使用real useragent,将视口设置为真实屏幕大小(默认值为400x300),并可能使用page.onError
回调检查错误。
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');
}
);