2014-12-04 233 views
0

我有一個數據庫的網址列表。如何使用for循環加載cefsharp中的多個url?

我傳遞的所有網址到瀏覽器中的foreach loop.But瀏覽器只考慮最後的URL和頁面只顯示去年的網址而不是以前的網址,我過去了。

怎麼解決呢?

示例代碼:

公共無效GetscreenshotBK(串HTML,字符串文件路徑) { HtmlWeb幅=新HtmlWeb(); HtmlAgilityPack.HtmlDocument doc = web.Load(filepath); 串路徑= @ 「// TD [@類= '案件']」; //跨度[@類= ' 「+ ClassToGet +」']

 //Xpath query the document for all matching nodes 
     HtmlNodeCollection nodes = doc.DocumentNode.SelectNodes(path); 

     //Get all the nodes in the node collection 
     // List<string> list = new List<string>(); 

     foreach (HtmlNode n in nodes) 
     { 
      //string path1 = @".//a";//span[@class='" + ClassToGet + "'] 

      //Xpath query the document for all matching nodes 

      IEnumerable<HtmlAgilityPack.HtmlNode> htmlnodevalues = n.Descendants("a"); 
      foreach (var node in htmlnodevalues) 
      {    
       string bkurl = node.GetAttributeValue("href",""); 
       Browser.Load(bkurl); 
       //list.Add(bkurl); 
      }} 
+0

你能粘貼到目前爲止的代碼嗎? – Yoshi 2014-12-04 09:18:48

+0

你想用加載的頁面做什麼?只要看看他們X秒? CefSharp一次只能查看一個頁面,除非您在新選項卡中打開它們。 – Yoshi 2014-12-04 10:21:23

+0

我只想通過javascript獲取一些操作的屏幕截圖。 – Sanjay 2014-12-04 13:23:58

回答

1

只可能對做這個工作另一個線程使用任務。因爲帶ChromiumWebBrowser控件的主線程需要可以自由加載和呈現網頁。

下面是如何使用新的方法的例子:

// Take screenshots of 3 web pages, and save the screenshots to C:\testX.png 
var takeScreenshots = GetScreenshots(Browser, new[] { "http://www.google.com/", "http://www.yahoo.com", "http://www.apple.com" }); 
takeScreenshots.ContinueWith((task) => { 
    int i = 0; 
    foreach (var bitmap in task.Result) 
     bitmap.Save(@"C:\test" + (i++).ToString() + ".png"); 
}); 
takeScreenshots.Start(); 

您需要將此方法添加到您的類:

public Task<List<Bitmap>> GetScreenshots(ChromiumWebBrowser browser, IEnumerable<string> urls) 
    { 
     return new Task<List<Bitmap>>(() => { 
      var bitmaps = new List<Bitmap>(); 

      var screenshotDone = new AutoResetEvent(false); 

      EventHandler<FrameLoadEndEventArgs> loaded = null; 
      loaded = (s, e) => { 
       // Start a new thread so CefSharp can finish loading and rendering the page. 
       if (e.IsMainFrame && e.Url != "about:blank") { 
        new System.Threading.Tasks.Task(() => { 
         System.Threading.Thread.Sleep(3000); 

         bitmaps.Add(ControlSnapshot.Snapshot(browser)); 

         screenshotDone.Set(); 
        }).Start(); 
       } 
      }; 

      browser.FrameLoadEnd += loaded; 

      foreach (string url in urls) { 
       // Wait until page finished loading and take a screenshot. 
       browser.Invoke(new MethodInvoker(() => { 
        browser.Load(url); 
       })); 

       screenshotDone.WaitOne(); 
      } 

      browser.FrameLoadEnd -= loaded; 

      return bitmaps; 
     }); 
    } 

隨着這些using聲明:

using System.Threading; 
using System.Threading.Tasks; 

您還需要添加一個新的文件來創建快照,ControlSnapshot.cs

using System; 
using System.Drawing; 
using System.Drawing.Imaging; 
using System.Runtime.InteropServices; 
using System.Windows.Forms; 

public class ControlSnapshot 
{ 
    public static Bitmap Snapshot(Control c) 
    { 
     int width = 0, height = 0; 
     IntPtr hwnd = IntPtr.Zero; 
     IntPtr dc = IntPtr.Zero; 
     c.Invoke(new MethodInvoker(() => { 
      width = c.ClientSize.Width; 
      height = c.ClientSize.Height; 
      hwnd = c.Handle; 
      dc = GetDC(hwnd); 
     })); 
     Bitmap bmp = new Bitmap(width, height, PixelFormat.Format32bppRgb); 
     if (dc != IntPtr.Zero) { 
      try { 
       using (Graphics g = Graphics.FromImage(bmp)) { 
        IntPtr bdc = g.GetHdc(); 
        try { 
         BitBlt(bdc, 0, 0, width, height, dc, 0, 0, TernaryRasterOperations.SRCCOPY); 
        } finally { 
         g.ReleaseHdc(bdc); 
        } 
       } 
      } finally { 
       ReleaseDC(hwnd, dc); 
      } 
     } 
     return bmp; 
    } 

    [DllImport("user32.dll", SetLastError = true)] 
    public static extern IntPtr GetDC(IntPtr hWnd); 

    [DllImport("user32.dll")] 
    public static extern int ReleaseDC(IntPtr hWnd, IntPtr hDC); 

    [DllImport("gdi32.dll")] 
    [return: MarshalAs(UnmanagedType.Bool)] 
    public static extern bool BitBlt(IntPtr hdc, int nXDest, int nYDest, int nWidth, int nHeight, IntPtr hdcSrc, int nXSrc, int nYSrc, TernaryRasterOperations dwRop); 

    public enum TernaryRasterOperations : uint 
    { 
     /// <summary>dest = source</summary> 
     SRCCOPY = 0x00CC0020, 
     /// <summary>dest = source OR dest</summary> 
     SRCPAINT = 0x00EE0086, 
     /// <summary>dest = source AND dest</summary> 
     SRCAND = 0x008800C6, 
     /// <summary>dest = source XOR dest</summary> 
     SRCINVERT = 0x00660046, 
     /// <summary>dest = source AND (NOT dest)</summary> 
     SRCERASE = 0x00440328, 
     /// <summary>dest = (NOT source)</summary> 
     NOTSRCCOPY = 0x00330008, 
     /// <summary>dest = (NOT src) AND (NOT dest)</summary> 
     NOTSRCERASE = 0x001100A6, 
     /// <summary>dest = (source AND pattern)</summary> 
     MERGECOPY = 0x00C000CA, 
     /// <summary>dest = (NOT source) OR dest</summary> 
     MERGEPAINT = 0x00BB0226, 
     /// <summary>dest = pattern</summary> 
     PATCOPY = 0x00F00021, 
     /// <summary>dest = DPSnoo</summary> 
     PATPAINT = 0x00FB0A09, 
     /// <summary>dest = pattern XOR dest</summary> 
     PATINVERT = 0x005A0049, 
     /// <summary>dest = (NOT dest)</summary> 
     DSTINVERT = 0x00550009, 
     /// <summary>dest = BLACK</summary> 
     BLACKNESS = 0x00000042, 
     /// <summary>dest = WHITE</summary> 
     WHITENESS = 0x00FF0062, 
     /// <summary> 
     /// Capture window as seen on screen. This includes layered windows 
     /// such as WPF windows with AllowsTransparency="true" 
     /// </summary> 
     CAPTUREBLT = 0x40000000 
    } 
} 
+0

解決方案如何使用它的控制檯應用程序.. – Sanjay 2014-12-17 16:06:30

+0

@Sanjay我糊塗了......你說你需要的WinForms的解決方案。 – Yoshi 2014-12-18 02:45:49

+1

@Sanjay也許你應該測試這一點,看看它是否適合你,接受的答案,然後再發布新問題的解決方案控制檯,因爲這將是完全不同的。 – Yoshi 2014-12-18 02:46:31