Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/objective-c/22.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript PhantomJS未加载动态内容_Javascript_Phantomjs - Fatal编程技术网

Javascript PhantomJS未加载动态内容

Javascript PhantomJS未加载动态内容,javascript,phantomjs,Javascript,Phantomjs,我正在尝试加载一个带有PhantomJS的页面,并将其保存为.png。但是,创建的png看起来与原始的不一样,并且缺少主体的大部分。在网上搜索时,大多数类似的问题都是因为没有等待足够长的时间来加载页面。然而,这并没有解决我的问题。这就是我正在运行的: var page = require('webpage').create(); var websiteAddress = 'http://poe.ninja/standard/currency'; page.settings.userAgent =

我正在尝试加载一个带有PhantomJS的页面,并将其保存为.png。但是,创建的png看起来与原始的不一样,并且缺少主体的大部分。在网上搜索时,大多数类似的问题都是因为没有等待足够长的时间来加载页面。然而,这并没有解决我的问题。这就是我正在运行的:

var page = require('webpage').create();
var websiteAddress = 'http://poe.ninja/standard/currency';
page.settings.userAgent = 'Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/37.0.2062.120 Safari/537.36';
//viewportSize being the actual size of the headless browser
page.viewportSize = { width: 1920, height: 1080 };
//the clipRect is the portion of the page you are taking a screenshot of
page.clipRect = { top: 0, left: 0, width: 1920, height: 1080 };

page.open(websiteAddress, function (status) {
    setTimeout(function(){
        page.render('output.png');
        phantom.exit();
    }, 5000); // 5 sec should be enough
});
我是做错了什么,还是这是幻影中的一个bug

下面是它应该是什么样子以及它实际上是什么样子的图像:

预期:
实际:

使用
page.onResourceRequested=function(request){}
page.onResourceReceived=function(response){}
回调来记录已启动和已完成的资源下载。一旦您的
onResourceReceived
回调检测到所有下载都已完成,则调用
page.render()
。这比5秒超时更可靠


我建议在调用
page.render()
之前等待0.5秒,因为最后一个挂起的资源可能会完成,但随后会触发额外的下载。

目标站点是一个React应用程序,它可能使用当前PhantomJS中不可用的一些较新的ES6 javascript语法(因为它使用的是较旧的QTWebkit渲染引擎)


解决方案是使用polyfill库来弥补这些缺少的方法,正如在下面的回答中所做的那样:

添加回调以检查是否有任何错误。这样做了!谢谢