获取 div class 高度

Get div class height

有没有办法获得 classC# 上的高度? 我正在使用 HTMLAgilitPack 来获取节点。这是我的代码。

private async void GetCldInfos()
    {
        string sURL = @"https://m.investing.com/economic-calendar/";
        using (HttpClient clientduplicate = new HttpClient())
        {
            clientduplicate.DefaultRequestHeaders.Add("User-Agent",
                "Mozilla/5.0 (compatible; MSIE 10.0; Windows NT 6.2; WOW64; Trident / 6.0)");

            using (HttpResponseMessage responseduplicate = await clientduplicate.GetAsync(sURL))
            using (HttpContent contentduplicate = responseduplicate.Content)
            {
                try
                {
                    string resultduplicate = await contentduplicate.ReadAsStringAsync();

                    var websiteduplicate = new HtmlDocument();
                    websiteduplicate.LoadHtml(resultduplicate);
                    var News = websiteduplicate.DocumentNode.Descendants("div").Where(o => o.GetAttributeValue("class", "") == "js-economic-calendar").Single();
                    //get height here
                }
                catch (Exception ex1)
                {
                    throw ex1.InnerException;
                }
            }
        }
    }

编辑:这是一张图片: 在图像中我似乎可以找到它的高度。有没有办法以编程方式获得该高度? 我想在滚动查看器中实现它。我已经禁用了浏览器滚动条,所以我可以使用我的。我需要设置 scrollviewers 高度以适应表单...

我不这么认为,因为高度是由浏览器计算的。

你必须先预渲染这种类型的 HTML 然后计算它。


更新: 如果你愿意使用 JavaScript 来获取高度,那很容易。

获取 div 包装您想要使用 slimscroll 的视图

function getsize() {
            var el = $('#divIdYouWantSize'),
//current eight of your div
               curHeight = el.height(),
//set div height using CSS style
               autoHeight = el.css('height', $(window).height() ).height();
//try animating the resize so it looks pretty
                el.height(curHeight).animate({ height: autoHeight }, 100);
};

这是一个使用无头浏览器 Watin 的解决方案,通过内联 javascript.

获取 DOM 元素的高度

首先使用 Visual Studio Nuget 包管理器控制台 安装 Watin,方法是执行:

PM> Install-Package WatiN

成功安装 Watin headless 浏览器后,您可以像这样使用它来导航到页面和 运行 一个简单的 javascript 来检索元素的高度:

using WatiN.Core;
...

private void buttonGetElementHeight_Click(object sender, EventArgs e)
{
    WatiN.Core.Settings.Instance.MakeNewIeInstanceVisible = false;
    IE browser = new IE();

    browser.GoToNoWait("https://m.investing.com/economic-calendar/");
    browser.WaitUntilContainsText("Filters");

    var height = browser.Eval("document.getElementsByClassName('js-economic-calendar')[0].offsetHeight");
    labelResult.Text = String.Format("Element height is {0}px", height);

    browser.ForceClose();
}

这是获取高度的工作按钮的屏幕截图:

编辑

请注意,这是一个测试,您必须为 C# Watin 对象 内联 javascript[=44= 实施一些错误处理] 正在评估中。

更新

以下是如何使用 Windows.UI.Xaml.Controls.WebView:

private async void webView1_LoadCompleted(object sender, NavigationEventArgs e)
{
    var offsetHeight = await webView1.InvokeScriptAsync("eval", new string[] {
        "document.getElementsByClassName('js-economic-calendar')[0].offsetHeight.toString()"
    });
    textBox.Text = offsetHeight;
}

private async void button_Click(object sender, RoutedEventArgs e)
{
    webView1.LoadCompleted += webView1_LoadCompleted;
    webView1.Navigate(new Uri("https://m.investing.com/economic-calendar/"));
}

我的工作屏幕截图: