gpt4 book ai didi

javascript - 无法使用 Phantomjs 代码示例在 instagram.com 上截图,为什么总是黑屏?

转载 作者:行者123 更新时间:2023-12-03 01:33:27 25 4
gpt4 key购买 nike

这是我的 phantomjs super 简单的代码(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

结果 - 始终是黑色屏幕截图,空白

最佳答案

问题是,当您截取屏幕截图时,该页面尚未处理。您应该等待页面完全加载(使用 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');
}
);

关于javascript - 无法使用 Phantomjs 代码示例在 instagram.com 上截图,为什么总是黑屏?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51191584/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com