我需要 WebBrowser 控件的正确页面高度。
在 Javascript 中,这是有效的:
var body = document.body;
var html = document.documentElement;
var height = Math.max( body.scrollHeight, body.offsetHeight, html.clientHeight, html.scrollHeight, html.offsetHeight );
SO上存在重复问题,建议采用以下方法:
webBrowser.Document.Body.ScrollRectangle.Height;
webBrowser.Document.GetElementsByTagName("body")[0].OffsetRectangle.Bottom;
webBrowser.Document.GetElementsByTagName("body")[0].OffsetRectangle.Height;
除了没有一个返回与 JS 代码相同的值。
我对 JS 值错误的可能性持开放态度,但我相当有信心它是正确的,因为滚动到页面底部并将 ScrollTop
位置添加到控件 height
产生相同的值。
webBrowser1.Document.GetElementsByTagName("HTML")[0].ScrollTop + webBrowser1.Height;
对于我的特定情况,C# 返回高度 510,而 JS 返回正确的高度 529。这种不一致导致我编写的自定义滚动条失效。
最佳答案
如果您有权访问该页面的代码,那么您可以将问题中的 Javascript 添加到该页面,然后从网络浏览器调用它并检索结果。
HTML
<html>
<head>
<script>
function GetPageHeight()
{
var body = document.body;
var html = document.documentElement;
var height = Math.max( body.scrollHeight, body.offsetHeight, html.clientHeight, html.scrollHeight, html.offsetHeight );
return height;
}
</script>
</head>
<body>
Test
</body>
</html>
C#
int height = (int)webBrowser1.Document.InvokeScript("GetPageHeight");
关于javascript - 获取 WebBrowser 控件的正确页面高度,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34288051/