Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

CefSharp - Get Value of HTML Element

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.

like image 836
Ravi Kiran Avatar asked Jun 08 '16 21:06

Ravi Kiran


2 Answers

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);
        }
    }
like image 156
i31nGo Avatar answered Oct 01 '22 08:10

i31nGo


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
}
like image 36
berkhan Avatar answered Oct 01 '22 07:10

berkhan