2017-06-21 158 views
2

我有一个问题如下。我使用CefSharp离线进行网页自动化,如下所示(我只打开一个页面): 1.打开页面并等待直至它呈现*。 2.使用EvaluateScriptAsync我把价值输入表单,然后用相同的方法,我点击网页上的按钮。 3.然后在这个网页上有一些JS检查结果并显示一条消息。 4.当显示信息时,我做了一个截图。 **CefSharp offscreen - 等待页面渲染

但是,我有两个问题: *我的sulution必须是Internet速度证明。并且正如我使用BrowserLoadingStateChanged事件和IsLoading方法,即使事件触发网页没有完全加载 - 当我启动EavluateScriptAsync方法时,由于页面没有完全加载,返回错误。当然,我可以像ThreadSleep这样的东西,但它并不总是工作 - 它强烈依赖于你的互联网速度。

**当我尝试制作屏幕截图时,它并不总是包含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和在不同的硬件页面上的所有测试中呈现。即使在离线的CefSharp演示中,也有建议使用线程睡眠来等待页面呈现。 – Mikisz

+0

如果某个进程将使用所有CPU,该怎么办?可能有一种方法可以检查Chrome的渲染应用/处理器/ therad是否已经变得闲置? – AgentFire