PhantomJS:获取页面的呈现尺寸
PhantomJS: getting the rendered dimensions of a page
当使用PhantomJS进行屏幕捕获时,其中大部分页面内容都是在通过JavaScript加载时或加载期间添加的,我遇到了问题。 调用 render() 会生成正确的图像,显示页面的完整内容,但评估 document.body.clientHeight 会返回一个很小的值,该值可能是添加任何内容之前的页面高度。
如何在 PhantomJS 渲染图像时获取图像的高度/宽度? 我不认为这是一个时间问题,我尝试交换事物的顺序或设置长时间的延迟以确保所有内容都完全加载。
var wp = require('webpage');
var page = wp.create();
page.viewportSize = { width: 1024, height: 768};
page.open(url, function (status) {
if (status === 'success') {
var f = "rendered.png";
//Produces an image with height 4073px
page.render(f);
//height is only 150
var height = page.evaluate(function() { return document.body.offsetHeight }),
width = page.evaluate(function() { return document.body.offsetWidth });
console.log(height,width);
}
});
尝试使用 page.onLoadFinished
回调:
var wp = require('webpage');
var page = wp.create();
page.viewportSize = { width: 1024, height: 768};
page.open(url);
page.onLoadFinished = function() {
var f = "rendered.png";
//Produces an image with height 4073px
page.render(f);
//height is only 150
var height = page.evaluate(function() { return document.body.offsetHeight }),
width = page.evaluate(function() { return document.body.offsetWidth });
console.log(height,width);
};
这应该在页面内容完成加载后返回正确的高度和宽度。我认为不同之处在于page.onLoadFinished
等待所有内容完成加载,而不仅仅是等同于success
状态的200 OK
响应。
相关文章:
- 从Spring应用程序运行PhantomJS以获取网页缩略图
- PhantomJs-从页面中获取值.evaluate+ajax(同步版本)
- 使用PhantomJS获取body onload javascript的值
- 无法从 PhantomJs 渲染命令中获取整个 png
- 如何使用PhantomJS从页面获取动态HTML和Javascript值
- 如何在 PhantomJS 中获取当前脚本名称
- 使用网桥获取随 PhantomJS 返回的状态代码
- PhantomJS:获取页面的呈现尺寸
- PhantomJS获取网址参数
- 如何在PhantomJS中获取JSessionID
- 从PhantomJS网站上的Ajax请求中获取状态代码
- 无法使用PhantomJS获取要单击的链接并切换到下一页
- 使用PhantomJS获取Javascript渲染的html
- 如何使用phantomjs在JS事件后获取更新的页面内容
- 如何远程获取在heroku上运行的phantomjs脚本的答案
- 在c#中使用PhantomJS获取截图
- Phantomjs:获取没有id的元素
- 如何使用asp.net和phantomjs获取页面的屏幕截图并将其返回给客户端
- 在PhantomJS中获取页面的标题
- 获取PhantomJS中IFrame的文档对象