Get div class height

c# css html-agility-pack uwp


Is there a way to get a class 's height on C#? I am using HTMLAgilitPack to get the nodes. Here is my code.

private async void GetCldInfos()
        string sURL = @"";
        using (HttpClient clientduplicate = new HttpClient())
                "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)
                    string resultduplicate = await contentduplicate.ReadAsStringAsync();

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

Edit: here is an image: here In the image I can seem to find it's height. Is there a way to get that height programmatically? I want to implement it in a scrollviewer. I have disabled the browsers scrollbars so I can use mine. And I need to set the scrollviewers height to fit the form...

3/5/2017 11:37:01 PM

Accepted Answer

This is a solution using the headless browser Watin to get the height of a DOM element using inline javascript.

First install Watin using Visual Studio Nuget Package Manager Console by executing:

PM> Install-Package WatiN

After the successfull installation of Watin headless browser, you can use it like this to navigate to a page and run a simple javascript to retrieve an element's height:

using WatiN.Core;

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


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


Here is a screen capture of the working button fetching the height:

Screen capture


Beware that this is a test and you have to implement some error handling for both C# Watin objects and inline javascript that is being evaluated.


Here is how to do it using Windows.UI.Xaml.Controls.WebView:

private async void webView1_LoadCompleted(object sender, NavigationEventArgs e)
    var offsetHeight = await webView1.InvokeScriptAsync("eval", new string[] {
    textBox.Text = offsetHeight;

private async void button_Click(object sender, RoutedEventArgs e)
    webView1.LoadCompleted += webView1_LoadCompleted;
    webView1.Navigate(new Uri(""));

My working screen capture:

WebView Screen Capture

3/6/2017 12:51:59 AM

Popular Answer

I don't think so, as the height is calculated by the browser.

you would have to prerender this type of HTML first then calculate it.

Update: If you are willing to get the height using JavaScript, then its easy.

Get the div that is wrapping your view that you want to use your 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);

Related Questions


Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow