2016-03-09 58 views
7

Webページを読み込むためにaCefSharp.Wpf.ChromiumWebBrowser(バージョン47.0.3.0)を使用しています。ページがロードされた後のある時点で、私はソースコードを取得したい。CefSharp WebブラウザからHTMLソースコードを取得する

私が呼んでいる:

wb.GetBrowser().MainFrame.GetSourceAsync() 

、それはすべてのソースコードを返すように見えていませんが(私は子フレームがあるためであると考えています)。

私が呼び出す場合:

wb.GetBrowser().MainFrame.ViewSource() 

私はそれが(インナーフレームを含む)すべてのソースコードを示しています見ることができます。

私はViewSource()と同じ結果を得たいと思います。ある人が正しい方向に私を向けることができますか?

アップデート - 追加されたコードの例

注:Webブラウザがあまりにも指しているアドレスだけまで働くと2016年10月3日を含めます。その後、それは私が見ているものではない異なるデータを表示することがあります。 frmSelection.xamlで

は、私は私はかなりこのDispatcherTimer解を得るとは思わない

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

} 
+0

もっとコードを共有できますか?私はあなたの問題を再現できません、 'ViewSource'と同じように' GetSourceAsync'で同じテキストを取得します。 'http:// stackoverflow.com'(' iframe'とメインフレームの2つのフレームがあります)に設定された 'Address'で試してみました。 –

+0

ありがとうございました。元の投稿にサンプルソースを追加しました。 – Scott

答えて

11

ファイルfrmSelection.xaml.csで

<cefSharp:ChromiumWebBrowser Name="wb" Grid.Column="1" Grid.Row="0" /> 

を提出。

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

私はViewSourceの出力とhtml変数内のテキストの差分を行なったし、それらが同じであるので、私はここにあなたの問題を再現することはできません。私はこのようにそれを行うだろう。

これは言った、私は、メインフレームがかなり遅れてロードされることに気づいたので、あなたは、メモ帳がソースにポップアップ表示されるまで、かなり長い間待たなければなりません。

+0

私のコードのフィードバックをありがとう、私はあなたの例を反映するように更新しました。 例を投稿してから別のコンピュータでコードを実行しましたが、同じ結果が得られます(両方とも完全なソースコードを返します)。私は自分のマシンで何か変わったことがあるとしか思えません。私はフォーマットをすることを検討します。 – Scott

1

私は、メインフレームではなく、フレームにあるアイテムをクリックしようとすると、同じ問題が発生していました。あなたはあなたのような何かを行うことができ、このメソッドが含まれているモジュールのフォーム上で「使用」している場合は

 public static IFrame GetFrame(this ChromiumWebBrowser browser, string FrameName) 
    { 
     IFrame frame = null; 

     var identifiers = browser.GetBrowser().GetFrameIdentifiers(); 

     foreach (var i in identifiers) 
     { 
      frame = browser.GetBrowser().GetFrame(i); 
      if (frame.Name == FrameName) 
       return frame; 
     } 

     return null; 
    } 

:あなたの答えに例を使用して、私は次の拡張メソッドを書いた

var frame = browser.GetFrame("nameofframe"); 
     if (frame != null) 
     { 
      string HTML = await frame.GetSourceAsync(); 
     } 

もちろん、これを使う前にページの読み込みが完了していることを確認する必要がありますが、私はそれをたくさん使う予定です。それが役に立てば幸い!

ジム

関連する問題