CefSharp - Get Value of HTML Element

user3397557 picture user3397557 · Jun 8, 2016 · Viewed 34.9k times · Source

How can I get the value of an HTML element with CefSharp?

I know how to do with this default WebBrowser Control:

Dim Elem As HtmlElement = WebBrowser1.Document.GetElementByID("id")

But I didn't find anything similar for CefSharp. The main reason I am using CefSharp is because part of the website is using iframes to store the source and default WebBrowser doesn't support it. Also, does CefSharp have an option to InvokeMember or similar call?

I'm using the latest release of CefSharp by the way.

Answer

RobbZ picture RobbZ · Jun 15, 2017

There is a really good example of how to do this in their FAQ.

https://github.com/cefsharp/CefSharp/wiki/Frequently-asked-questions#2-how-do-you-call-a-javascript-method-that-return-a-result

Here is the code for the lazy. Pretty self explanatory and it worked well for me.

string script = string.Format("document.getElementById('startMonth').value;");
browser.EvaluateScriptAsync(script).ContinueWith(x =>
        {
            var response = x.Result;

            if (response.Success && response.Result != null)
            {
            var startDate = response.Result;
            //startDate is the value of a HTML element.
        }      
    });