2017-06-21 15 views
2

私は以下のような問題があります。私は以下のようにCefSharpオフスクリーンをWebページのオートメーションに使用します(私は1つの同じページだけを開きます)。 1.ページを開き、レンダリングするまで待ってください。 2. EvaluateScriptAsyncを使用して、入力フォームに値を入力してから、同じ方法でWebページのボタンをクリックします。 3.このWebページには結果を確認してメッ​​セージを表示するJSがあります。 4.メッセージが表示されたら、スクリーンショットを作成します。 **CefSharpオフスクリーン - レンダリングのためにページを待つ

しかし、私には2つの問題があります。 *私の搾汁はインターネット速度の証拠でなければなりません。 BrowserLoadingStateChangedイベントとIsLoadingメソッドを使用すると、Webページが完全にロードされなかったにもかかわらず、EavluateScriptAsyncメソッドを開始したときにページが完全にロードされなかったためにエラーが返されます。確かに、私はThreadSleepのようなsthを置くことができますが、それは必ずしも機能しません - あなたのインターネットの速度に強く依存しています。

**スクリーンショットを作成しようとすると、JSで表示される結果メッセージが常に含まれるとは限りません。メッセージの代わりにローディングサークルが表示されることがあります。そしてここでも私はTHreadSleepを使うことができますが、それはいつもうまくいくわけではありません。

ご意見はありますか?前もって感謝します。

private static void BrowserLoadingStateChanged(object sender, LoadingStateChangedEventArgs e) 
     { 
      // Check to see if loading is complete - this event is called twice, one when loading starts 
      // second time when it's finished 
      // (rather than an iframe within the main frame). 
      if (!e.IsLoading) 
      { 
       // Remove the load event handler, because we only want one snapshot of the initial page. 
       browser.LoadingStateChanged -= BrowserLoadingStateChanged; 

       Thread.Sleep(1800); // e. g. but it isn't a solution in fact 

       var scriptTask = browser.EvaluateScriptAsync("document.getElementById('b-7').value = 'something'"); 



       scriptTask = browser.EvaluateScriptAsync("document.getElementById('b-8').click()"); 

       //scriptTask.Wait(); 

       if (browser.IsLoading == false) 
       { 
        scriptTask.ContinueWith(t => 
        { 

         //Give the browser a little time to render 
         //Thread.Sleep(500); 
         Thread.Sleep(500); // still not a solution 

         // Wait for the screenshot to be taken. 
         var task = browser.ScreenshotAsync(); 
         task.ContinueWith(x => 
         { 
          // Make a file to save it to (e.g. C:\Users\jan\Desktop\CefSharp screenshot.png) 
          var screenshotPath = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.Desktop), "CefSharp screenshot.png"); 

          Console.WriteLine(); 
          Console.WriteLine("Screenshot ready. Saving to {0}", screenshotPath); 

          // Save the Bitmap to the path. 
          // The image type is auto-detected via the ".png" extension. 
          task.Result.Save(screenshotPath); 

          // We no longer need the Bitmap. 
          // Dispose it to avoid keeping the memory alive. Especially important in 32-bit applications. 
          task.Result.Dispose(); 

          Console.WriteLine("Screenshot saved. Launching your default image viewer..."); 

          // Tell Windows to launch the saved image. 
          Process.Start(screenshotPath); 

          Console.WriteLine("Image viewer launched. Press any key to exit."); 
         }, TaskScheduler.Default); 
        }); 
       } 


      } 
     } 

答えて

0

私の場合、最高の解決策は、javascriptを使用してid要素が存在するかどうかを確認することでした。はいの場合、ページがロードされます。

+0

読み込まれましたが、まだレンダリングされていない可能性があります。 – AgentFire

+0

実際にロードされているがレンダリングされないかもしれないが、ロードされている情報に基づいていて、スレッドスリープ200と異なるハードウェアページのすべてのテストでsoluttionを使って終了した。 CefSharpのデモ画面でも、ページスリープが表示されるまでスレッドスリープを使用することを提案しています。 – Mikisz

+0

一部のプロセスですべてのCPUが使用される場合はどうなりますか? Chromeのレンダリングアプリ/プロセッサ/ theradがアイドル状態になっているかどうかを確認する方法がありますか? – AgentFire

関連する問題