首页 文章

CefSharp - 获取HTML元素的 Value

提问于
浏览
3

如何使用CefSharp获取HTML元素的值?

我知道如何处理这个默认的WebBrowser控件:

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

但我没有找到类似的CefSharp . 我使用CefSharp的主要原因是因为网站的一部分使用iframe来存储源,而默认的WebBrowser不支持它 . 此外,CefSharp是否可以选择InvokeMember或类似的呼叫?

顺便说一下,我正在使用最新版本的CefSharp .

3 回答

  • -1

    使用CefSharp,您可以通过javascript获取元素的 Value .

    例如,

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

    关于javascript,你可以从w3s学到它 .

    而且我认为你应该阅读this passage .

    玩得开心 .

  • 0

    在他们的常见问题解答中有一个很好的例子 .

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

    这是懒惰的代码 . 非常自我解释,它对我来说很有效 .

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

    这是对我有用的唯一方法,版本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);
            }
        }
    

相关问题