Get div class height

c# css html-agility-pack uwp

Question

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

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.

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

    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 screenshot of the active button that retrieves the height:

Screen capture

EDIT

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.

UPDATE

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[] {
        "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 effective screen grab is:

WebView Screen Capture

4
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





Related

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