YSFKBDY
YSFKBDY

Reputation: 886

Getting HTML from Cefsharp Browser

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.

Upvotes: 3

Views: 10702

Answers (2)

serkan
serkan

Reputation: 11

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

Upvotes: 1

Hakros
Hakros

Reputation: 81

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 !
        }
    }

Upvotes: 8

Related Questions