我写了一个幻影脚本,使用节点拍摄网页的截图。我试图截图的页面包含一个重要的外部脚本,它将变量添加到全局窗口对象中。即。window.__components。
问题是,这从来没有设定过。它在浏览器中工作得很好,但在phantomJS中就会死掉。我修改了脚本,在打开页面之前注入脚本,我添加了一个检查,以确保组件对象在打开页面之前已经添加,但仍然失败。
有什么想法吗?
function injectExternalScripts(page, ph, url) {
page.includeJs('http://external-script', ()=>{
page.evaluate(() => {
return window;
}, function(result) {
if(result.__components) {
openPage(page, ph, url);
}
});
});
}
function openPage(page, ph, url) {
page.open(url, (status)=>{
// errors returned from page
// window.__components is null
});
}发布于 2016-01-12 14:20:00
您只能从PhantomJS中的页面上下文返回原始对象。window和DOM节点不是原始对象,如果__components也不是原始对象,那么您也不能返回该对象。
您可以检查是否设置了__components:
page.evaluate(() => {
return !!window.__components;
}, function(result) {
if(result) {
openPage(page, ph, url);
}
});https://stackoverflow.com/questions/34742689
复制相似问题