本文介绍了从Cefsharp浏览器获取HTML的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在WinForm项目中使用CefSharp v55.0。页面加载后,我想从中获取HTML代码。为此,我正在使用它:

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

对于交叉检查,我也称测试。 ViewSource()方法,以查看GetSourceAsync方法是否获取整个代码。

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

不幸的是,代码不同。 ViewSource正在获取整个代码,但是GetSourceAsync并没有通过页面中生成的javascript获取代码。

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

请引导我一种获取页面源代码的方法,例如ViewSource,或告诉我如何捕获此ViewSource方法的临时文件。

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.

干杯。

推荐答案

尝试一下,它对我有用:

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

这篇关于从Cefsharp浏览器获取HTML的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-04 16:01