CefSharp - Get Value of HTML Element
Asked Answered
V

5

10

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.

Vallombrosa answered 8/6, 2016 at 21:7 Comment(3)
You have to cast the document to MSHTML.IHTMLDocument2, if I recall correctly.Penneypenni
Read the CefSharp FAQ for starters. You need to remember it's not a likely for like replacement for the built in control.Ousel
have you tried HtmlElement Elem = webBrowser1.Document.GetElementById("id");Demonstrable
C
14

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.
        }      
    });
Cornuted answered 15/6, 2017 at 14:4 Comment(4)
Have you tested this?Unfortunate
I have never managed to get browser.EvaluateScriptAsync(script) to work yet. browser.GetMainFrame.ExecuteJavascriptAsync(script) i have.Unfortunate
await _browser.GetMainFrame().EvaluateScriptAsync(script).ContinueWith(x => { var response = x.Result; if (response.Success) { //do ur stuff } }); worked for me, otherwise here is the official docBianca
I think it is confusing because the full name of the property is x.Result.ResultToft
I
3

this is the only way that worked for me, version 57.0.0.0..

((CefSharp.Wpf.ChromiumWebBrowser)chromeBrowser).FrameLoadEnd += Browser_FrameLoadEnd;

....

async void Browser_FrameLoadEnd(object sender, CefSharp.FrameLoadEndEventArgs e)
    {            
        Console.WriteLine("cef-"+e.Url);

        if (e.Frame.IsMain)
        {                 
            string HTML = await e.Frame.GetSourceAsync();               
            Console.WriteLine(HTML);
        }
    }
Islam answered 18/1, 2018 at 7:51 Comment(0)
A
2

This worked for me. You can modify it by yourself.

private async void TEST()
{
    string script = "document.getElementsByClassName('glass')[0]['firstElementChild']['firstChild']['wholeText']";
    JavascriptResponse response = await browser.EvaluateScriptAsync(script);
    label1.Text = response.Result.ToString();
}

Maybe this can do your job.

private async void TEST()
{
    string script = "Document.GetElementByID('id').value";
    JavascriptResponse response = await browser.EvaluateScriptAsync(script);
    string resultS = response.Result.ToString(); // whatever you need
}
Admonish answered 11/3, 2020 at 1:57 Comment(0)
C
0

With CefSharp,you can get elements' value by javascript.

For example,

m_browser.ExecuteScriptAsync("document.GetElementById('id1');");

About javascript,you can learn it from w3s.

And I think you should read this passage.

Have fun.

Clitoris answered 19/6, 2016 at 6:27 Comment(1)
This does not return any value.Eugenieeugenio
L
0
string script = @"document.getElementById('id_element').style;";
browser.EvaluateScriptAsync(script).ContinueWith(x=> {
    var response = x.Result;
    if (response.Success && response.Result != null)
    {
        System.Dynamic.ExpandoObject abc = (System.Dynamic.ExpandoObject)response.Result;
        foreach (KeyValuePair<string,object> item in abc)
        {
            string key = item.Key.ToString();
            string value = item.Value.ToString();
        }
    }
});

It working for me.

Leclerc answered 16/3, 2020 at 3:19 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.