简体   繁体   English

PhantomJS:获取页面的渲染尺寸

[英]PhantomJS: getting the rendered dimensions of a page

When using PhantomJS for screen capture where most of a page's content is getting added on or during load via JavaScript, I run into a problem. 当使用PhantomJS进行屏幕捕获时,大多数页面的内容都是通过JavaScript加载或加载的,我遇到了一个问题。 Calling render() produces the correct image, showing the full content of the page, but evaluating document.body.clientHeight returns a tiny value that is presumably the page's height before any content gets added. 调用render()会生成正确的图像,显​​示页面的完整内容,但是评估document.body.clientHeight会返回一个很小的值,可能是在添加任何内容之前页面的高度。

How can I get the height/width of the image as PhantomJS is rendering it? 当PhantomJS渲染图像时,如何获得图像的高度/宽度? I don't think it's a timing issue, I've tried swapping the order of things or setting long delays to ensure everything is totally loaded. 我不认为这是一个时间问题,我已经尝试交换事物的顺序或设置长时间延迟,以确保一切都完全加载。

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);

    }

});

Try using the page.onLoadFinished callback: 尝试使用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);
};

This should return the correct height and width after the page content has finished loading. 这应该在页面内容完成加载后返回正确的高度和宽度。 I think the difference is that page.onLoadFinished waits for all content to finish loading and not just for a 200 OK response which is equivalent to the success status. 我认为不同之处在于page.onLoadFinished等待所有内容完成加载,而不仅仅是200 OK响应,这相当于success状态。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM