最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

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

programmeradmin3浏览0评论

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 (mand-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

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

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 (mand-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

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

Share Improve this question edited Jul 6, 2015 at 14:56 Fábio Linhares asked Jul 6, 2015 at 12:13 Fábio LinharesFábio Linhares 3555 silver badges13 bronze badges 3
  • What are you expecting to get, if there's no actual browser? Why won't the CMD line window height work simply for testing purposes? Consider an edit to your post to clarify. – jamesmortensen Commented Jul 6, 2015 at 12:15
  • Why wouldn't the height of the CMD window work as a substitute, since Phantom doesn't have an actual physical browser? – jamesmortensen Commented Jul 6, 2015 at 12:32
  • What does CMD height mean? What values do you get and what values do you expect? Please show a plete example script. – Artjom B. Commented Jul 6, 2015 at 12:47
Add a ment  | 

1 Answer 1

Reset to default 7

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

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

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

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.
发布评论

评论列表(0)

  1. 暂无评论