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

元气小坏坏 提交于 2019-12-05 21:46:08

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(). 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.
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!