从CefSharp Web浏览器获取HTML源代码

Sco*_*ott 13 c# wpf cefsharp

我正在使用aCefSharp.Wpf.ChromiumWebBrowser(版本47.0.3.0)来加载网页.页面加载后的一些点我想得到源代码.

我打过电话:

wb.GetBrowser().MainFrame.GetSourceAsync()
Run Code Online (Sandbox Code Playgroud)

但它似乎没有返回所有的源代码(我相信这是因为有子帧).

如果我打电话:

wb.GetBrowser().MainFrame.ViewSource() 
Run Code Online (Sandbox Code Playgroud)

我可以看到它列出了所有的源代码(包括内部框架).

我想得到与ViewSource()相同的结果.有人能指出我正确的方向吗?

更新 - 添加代码示例

注意:Web浏览器指向的地址也将仅适用于2016年10月10日.之后,它可能会显示不同的数据,这不是我要看的.

在frmSelection.xaml文件中

<cefSharp:ChromiumWebBrowser Name="wb" Grid.Column="1" Grid.Row="0" />
Run Code Online (Sandbox Code Playgroud)

在frmSelection.xaml.cs文件中

public partial class frmSelection : UserControl
{
    private System.Windows.Threading.DispatcherTimer wbTimer = new System.Windows.Threading.DispatcherTimer();

    public frmSelection()
    {

         InitializeComponent();

         // This timer will start when a web page has been loaded.
         // It will wait 4 seconds and then call wbTimer_Tick which 
         // will then see if data can be extracted from the web page.
         wbTimer.Interval = new TimeSpan(0, 0, 4);
         wbTimer.Tick += new EventHandler(wbTimer_Tick);

         wb.Address = "http://www.racingpost.com/horses2/cards/card.sd?race_id=644222&r_date=2016-03-10#raceTabs=sc_";

         wb.FrameLoadEnd += new EventHandler<CefSharp.FrameLoadEndEventArgs>(wb_FrameLoadEnd);

    }

        void wb_FrameLoadEnd(object sender, CefSharp.FrameLoadEndEventArgs e)
        {
            if (wbTimer.IsEnabled)
                wbTimer.Stop();

            wbTimer.Start();
        }

    void wbTimer_Tick(object sender, EventArgs e)
    {
        wbTimer.Stop();
        string html = GetHTMLFromWebBrowser();
    }

    private string GetHTMLFromWebBrowser()
    {
         // call the ViewSource method which will open up notepad and display the html.
         // this is just so I can compare it to the html returned in GetSourceAsync()
         // This is displaying all the html code (including child frames)
            wb.GetBrowser().MainFrame.ViewSource();

         // Get the html source code from the main Frame.
            // This is displaying only code in the main frame and not any child frames of it.
            Task<String> taskHtml = wb.GetBrowser().MainFrame.GetSourceAsync();

            string response = taskHtml.Result;
     return response;
  }

}
Run Code Online (Sandbox Code Playgroud)

Sza*_*zsi 17

我认为我没有得到这个DispatcherTimer解决方案.我会这样做:

public frmSelection()
{
    InitializeComponent();

    wb.FrameLoadEnd += WebBrowserFrameLoadEnded;
    wb.Address = "http://www.racingpost.com/horses2/cards/card.sd?race_id=644222&r_date=2016-03-10#raceTabs=sc_";
}

private void WebBrowserFrameLoadEnded(object sender, FrameLoadEndEventArgs e)
{
    if (e.Frame.IsMain)
    {
        wb.ViewSource();
        wb.GetSourceAsync().ContinueWith(taskHtml =>
        {
            var html = taskHtml.Result;
        });
    }
}
Run Code Online (Sandbox Code Playgroud)

ViewSourcehtml变量的输出和文本做了一个差异,它们是相同的,所以我不能在这里重现你的问题.

这说,我注意到主框架很晚才加载,所以你必须等待一段时间,直到记事本弹出源代码.