Getting HTML from Cefsharp Browser

大憨熊 提交于 2019-12-08 07:32:57

问题


I am using CefSharp v55.0 in my WinForm project. After the page is loaded, I want to get HTML code from it. And for that I am using this:

private void WebBrowserFrameLoadEnded(object sender, FrameLoadEndEventArgs e)
{
    if (e.Frame.IsMain)
    {
        test.ViewSource();
        test.GetSourceAsync().ContinueWith(code =>
        {
            var html = code.Result;
        });
    }
}

And for the crosscheck, I am also calling test.ViewSource() method, to see that if GetSourceAsync method is getting the whole code or not.

Unfortunetely, codes are different. ViewSource is getting the whole code, but GetSourceAsync is not getting codes by javascript generated in the page.

Plase lead me a way to get source code of the page like ViewSource, or tell me how to capture this ViewSource method's temp file.

Cheers.


回答1:


Try this, it works for me:

    public void showSource()  // <<<<<<<<<<<<<<<<<<<<<<<<<< Call this function
    {
        Task ts = getSource();
    }

    private async Task getSource()
    {
        try
        {
            //
            string source = await chromeBrowser.GetBrowser().MainFrame.GetSourceAsync();
            //
            string f = Application.StartupPath + "\\currentSource.txt";
            //
            StreamWriter wr = new StreamWriter(f, false, System.Text.Encoding.Default);
            wr.Write(source);
            wr.Close();
            //
            System.Diagnostics.Process.Start(f);
            //
        }
        catch (Exception)
        {
            //Error !
        }
    }



回答2:


VB.Net:

Sub ShowSource()
        Dim ts As task = getSource()
End Sub


Private Async Function getSource() As Task
    Dim source As String = Await wb.GetBrowser().MainFrame.GetSourceAsync()


    Dim f As String = Application.StartupPath + "/currentSource.txt"

    Dim wr As StreamWriter = New StreamWriter(f, False, System.Text.Encoding.Default)
    wr.Write(source)
    wr.Close()

    System.Diagnostics.Process.Start(f)


End Function


来源:https://stackoverflow.com/questions/43451312/getting-html-from-cefsharp-browser

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