如何在Phantomjs(javascript)中获取完整html页面的高度? [英] How to get the height of a full html page in Phantomjs (javascript)?

查看:339
本文介绍了如何在Phantomjs(javascript)中获取完整html页面的高度?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您已尝试过所有这些:

document.body.scrollHeight
document.body.offsetHeight
document.documentElement.clientHeight
document.documentElement.scrollHeight
document.documentElement.offsetHeight

这些工作在普通浏览器中但在Phantomjs中我得到CMD(命令行窗口)高度..我想得到高度,以便我可以在代码中稍后裁剪屏幕截图..和高度页面必须在普通浏览器上查看

These work in a normal browser but in Phantomjs I get the CMD (command-line window) height.. I want to get the height so that I can crop a screenshot later in the code.. and the height of the page must be as it is being viewed on a normal browser

我得到300像素,我想得到完整的html页面高度(取决于URL的不同) )..

I'm getting 300 pixels and I want to get the full html page height (that varies dependent on the URL)..

推荐答案

这些值提供与其他浏览器一样的预期值。完整示例:

Those values provide the expected values as with other browsers. Full example:

var page = require('webpage').create();

var url = 'http://www.stackoverflow.com/';

page.open(url, function(){
    console.log(page.evaluate(function(){
        return JSON.stringify({
            "document.body.scrollHeight": document.body.scrollHeight,
            "document.body.offsetHeight": document.body.offsetHeight,
            "document.documentElement.clientHeight": document.documentElement.clientHeight,
            "document.documentElement.scrollHeight": document.documentElement.scrollHeight
        }, undefined, 4);
    }));
    phantom.exit();
});

输出:


{
    "document.body.scrollHeight": 8777,
    "document.body.offsetHeight": 8777,
    "document.documentElement.clientHeight": 300,
    "document.documentElement.scrollHeight": 8777
}

原因可能不是您的情况:

Reasons why it might not be the case for your:


  • 只能通过 page.evaluate()访问DOM。在 page.evaluate()之外存在文档对象,但它只是一个虚拟对象。

  • PhantomJS的默认视口为400x300像素。如果网页有响应,那么它只会使用此尺寸。

  • 与上面的点一起,< body> 可能不是可滚动的,但只有一些具有所有(可滚动)内容的子元素。在这种情况下,每个值都等于视口高度。

  • The DOM is only accessible through page.evaluate(). There exists a document object outside of page.evaluate(), but it is only a dummy.
  • PhantomJS has a default viewport of 400x300 pixels. If the web page is responsive, then it will only use this size.
  • Together with the point above, the <body> may not be scrollable, but only some child element that has all the (scrollable) content. In which case every value is equal to the viewport height.

这篇关于如何在Phantomjs(javascript)中获取完整html页面的高度?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆