简体   繁体   English

如何在Phantomjs(javascript)中获取完整html页面的高度?

[英]How to get the height of a full html page in Phantomjs (javascript)?

Hi have tried all of these: 您好过尝试过所有这些:

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

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 这些工作在普通的浏览器中,但在Phantomjs中,我得到CMD(命令行窗口)高度..我想获得高度,以便我可以在代码中稍后裁剪屏幕截图..并且页面的高度必须为它正在普通浏览器上查看

I'm getting 300 pixels and I want to get the full html page height (that varies dependent on the URL).. 我有300像素,我想得到完整的html页面高度(取决于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();
});

Output: 输出:

{
    "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: 您可能不是这样的原因:

  • The DOM is only accessible through page.evaluate() . DOM只能通过page.evaluate()访问。 There exists a document object outside of page.evaluate() , but it is only a dummy. page.evaluate()之外存在一个document对象,但它只是一个虚拟对象。
  • PhantomJS has a default viewport of 400x300 pixels. PhantomJS的默认视口为400x300像素。 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. 与上面的点一起, <body>可能不是可滚动的,而只是一些具有所有(可滚动)内容的子元素。 In which case every value is equal to the viewport height. 在这种情况下,每个值都等于视口高度。

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

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