gpt4 book ai didi

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

转载 作者:塔克拉玛干 更新时间:2023-11-02 22:32:36 24 4
gpt4 key购买 nike

您已经尝试了所有这些:

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

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

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

最佳答案

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

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}

您的情况可能并非如此的原因:

  • DOM 只能通过 page.evaluate() 访问.存在一个 document page.evaluate() 之外的对象, 但它只是一个假人。
  • PhantomJS 的默认视口(viewport)为 400x300 像素。如果网页是响应式的,那么它只会使用这个尺寸。
  • 连同上述几点,<body>可能不可滚动,但只有一些具有所有(可滚动)内容的子元素。在这种情况下,每个值都等于视口(viewport)高度。

关于javascript - 如何在 Phantomjs (javascript) 中获取完整 html 页面的高度?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31245553/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com