Javascript 虚节点未传递page.evaluate函数的返回值
我试图提取网页的特定元素,并将其保存为本地图像 node.js代码,使用幻影节点:Javascript 虚节点未传递page.evaluate函数的返回值,javascript,node.js,phantomjs,rect,Javascript,Node.js,Phantomjs,Rect,我试图提取网页的特定元素,并将其保存为本地图像 node.js代码,使用幻影节点: var phantom = require('phantom'); phantom.create().then(function(ph) { ph.createPage().then(function(page) { page.property('viewportSize', {width: 1600, height: 900}); page.open('http://stackover
var phantom = require('phantom');
phantom.create().then(function(ph) {
ph.createPage().then(function(page) {
page.property('viewportSize', {width: 1600, height: 900});
page.open('http://stackoverflow.com/questions/37570827/saving-element-of-webpage-as-an-image-using-js').then(function(status) {
if (status == 'success') {
page.evaluate(function() {
return document.getElementById("sidebar").getBoundingClientRect();
}).then(function(rect){
console.log(rect);
page.property('clipRect', rect);
page.render("question2.png");
});
}
page.close();
ph.exit();
});
});
});
log(rect)每次运行时都会打印不同的值。我不明白为什么会这样,但不管怎样,我猜我的边栏边界rect的return语句不起作用。代码有问题吗
编辑:实际上,在进一步调试之后,似乎正确返回了rect,但没有设置为clipRect 问题是page.render调用在page.close()发生之前没有足够的时间完成 此代码解决了以下问题:
var phantom = require('phantom');
phantom.create().then(function(ph) {
ph.createPage().then(function(page) {
page.open('http://stackoverflow.com/questions/37570827/saving-element-of-webpage-as-an-image-using-js').then(function(status) {
if (status == 'success') {
page.evaluate(function() {
return document.getElementById("sidebar").getBoundingClientRect();
}).then(function(rect){
page.property('clipRect', rect);
page.render("question.png").then(function(){
page.close();
ph.exit();
});
});
}
});
});
});
是的,每次打印不同的值是有意义的,因为内容总是在变化。问题是它总是呈现整个页面,而不是侧边栏元素。我需要页面的特定部分。您是否尝试在单独的
中调用render
,然后调用回调?@ArtjomB。看这里:这是我想要的工作方式只是幻影。js@ArtjomB. 是的,没有任何改变。