Selenium/java:如何获得总页面长度 [英] Selenium / java: how to get get total page length
问题描述
使用 Selenium Java 绑定.
Using Selenium Java bindings.
我正在尝试编写一个简短的模块来获取浏览器页面的完整屏幕截图(我知道 AShot 存在但它没有得到维护并且对我来说有一些问题).
I'm trying to write a short module that will take a full screenshot of a browser page (I know AShot exists but it is not maintained and has some issues for me).
作为其中的一部分,我想知道我正在处理的整页长度是多少,以便我可以计算我需要拍摄的照片数量,以及底部的任何余数.有很多示例展示了如何滚动到页面底部(例如 这个问题),但我想知道是否有办法让 JavascriptExecutor
返回页面长度的值,以便我可以使用它在我的脚本中?
As part of this I would like to know what the full page length is that I'm dealing with so I can calculate the number of shots I need to take, and any remainder at the bottom. There are many examples out there showing how to scroll to the bottom of a page (e.g. this question), but I am wondering if there is a way to make the JavascriptExecutor
return the value of the page length so I can use it in my script?
推荐答案
您需要 scrollHeight
JavascriptExecutor js = (JavascriptExecutor) driver;
js.executeScript("return document.documentElement.scrollHeight");
在进一步寻找更强大的解决方案之后,这个 Javascript 代码(我从同一个 网站) 应该提供更好的结果:
After looking further for a more robust solution, this Javascript-code (which I took from the same website) should provide a better result:
let scrollHeight = Math.max(
document.body.scrollHeight, document.documentElement.scrollHeight,
document.body.offsetHeight, document.documentElement.offsetHeight,
document.body.clientHeight, document.documentElement.clientHeight
);
return scrollHeight;
这篇关于Selenium/java:如何获得总页面长度的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!