Get div class height

c# css html-agility-pack uwp


Is it possible to get aclass on's heightC# ? I useHTMLAgilitPack obtaining the nodes. This 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;

Here is a picture for editing: here I think I can discern its height in the photograph. Is there a programmed approach to get that height? I want to use a scrollviewer to accomplish it. In order to utilize my own scrollbars, I have turned off the browser's. Additionally, I must adjust the scrollviewers' height to match the form.

3/5/2017 11:37:01 PM

Accepted Answer

This is a method for obtaining the height of a DOM element using inline JavaScript using the headless browser Watin.

Install Watin first using Console for Visual Studio Nuget Package Manager by carrying out:

PM> Install-Package WatiN

After the Watin headless browser has been installed successfully, you can use it in the following way to access a website and execute a simple java script to get 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 screenshot of the active button that retrieves the height:

Screen capture


You must provide some error handling for both the Watin objects in C# and embedded JavaScript that are being evaluated since this is a test.


How to do it usingWindows.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 effective screen grab is:

WebView Screen Capture

3/6/2017 12:51:59 AM

Popular Answer

I don't believe that is the case since the browser determines the height.

This kind of HTML would need to be prerendered before being calculated.

Update: Using JavaScript, getting the height is simple if you want it.

Obtain the div that surrounds the view you wish to utilize with 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