Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 369
YSFKBDY Avatar asked Mar 17 '26 08:03

YSFKBDY


1 Answers

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 !
        }
    }
like image 100
Hakros Avatar answered Mar 18 '26 22:03

Hakros



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!