Get div class height

c# css html-agility-pack uwp

Question

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 = @"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;
                }
            }
        }
    }

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...

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();

    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();
}

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

Screen capture

EDIT

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.

UPDATE

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[] {
        "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/"));
}

My working screen capture:

WebView Screen Capture


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);
};


Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Is this KB legal? Yes, learn why
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Is this KB legal? Yes, learn why