CefSharp - Get Value of HTML Element

江枫思渺然 提交于 2020-01-02 05:35:09

问题


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.


回答1:


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



回答2:


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



回答3:


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.



来源:https://stackoverflow.com/questions/37712962/cefsharp-get-value-of-html-element

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!