title: View Generated Source (After AJAX/JavaScript) in C#
body: <p>Is there a way to view the generated source of a web page (the code after all AJAX calls and JavaScript DOM manipulations have taken place) from a C# application without opening up a browser from the code?</p>
  
  <p>Viewing the initial page using a <a href="http://www.csharp-station.com/HowTo/HttpWebFetch.aspx" rel="noreferrer">WebRequest</a> or <a href="http://www.codersource.net/csharp_screen_scraping.html" rel="noreferrer">WebClient</a> object works ok, but if the page makes extensive use of JavaScript to alter the DOM on page load, then these don't provide an accurate picture of the page.</p>
  
  <p>I have tried using <a href="http://seleniumhq.org/" rel="noreferrer">Selenium</a> and <a href="http://watin.sourceforge.net/" rel="noreferrer">Watin</a> UI testing frameworks and they work perfectly, supplying the generated source as it appears after all JavaScript manipulations are completed.  Unfortunately, they do this by opening up an actual web browser, which is very slow.  I've implemented a selenium server which offloads this work to another machine, but there is still a substantial delay.</p>
  
  <p>Is there a .Net library that will load and parse a page (like a browser) and spit out the generated code?  Clearly, Google and Yahoo aren't opening up browsers for every page they want to spider (of course they may have more resources than me...).  </p>
  
  <p>Is there such a library or am I out of luck unless I'm willing to dissect the source code of an open source browser?</p>
  
  <p><strong>SOLUTION</strong></p>
  
  <p>Well, thank you everyone for you're help.  I have a working solution that is about 10X faster then Selenium. Woo!</p>
  
  <p>Thanks to this <a href="http://www.beansoftware.com/ASP.NET-Tutorials/Get-Web-Site-Thumbnail-Image.aspx" rel="noreferrer">old article from beansoftware</a> I was able to use the System.Windows.Forms.WebBrowser control to download the page and parse it, then give em the generated source.  Even though the control is in Windows.Forms, you can still run it from Asp.Net (which is what I'm doing), just remember to add System.Window.Forms to your project references.</p>
  
  <p>There are two notable things about the code.  First, the WebBrowser control is called in a new thread.  This is because it must run on a <a href="http://msdn.microsoft.com/en-us/library/ms680112%28VS.85%29.aspx" rel="noreferrer">single threaded apartment</a>.</p>
  
  <p>Second, the GeneratedSource variable is set in two places.  This is not due to an intelligent design decision :)  I'm still working on it and will update this answer when I'm done.  wb_DocumentCompleted() is called multiple times.  First when the initial HTML is downloaded, then again when the first round of JavaScript completes.  Unfortunately, the site I'm scraping has 3 different loading stages.  1) Load initial HTML 2) Do first round of JavaScript DOM manipulation 3) pause for half a second then do a second round of JS DOM manipulation.</p>
  
  <p>For some reason, the second round isn't cause by the wb_DocumentCompleted() function, but it is always caught when wb.ReadyState == Complete.  So why not remove it from wb_DocumentCompleted()? I'm still not sure why it isn't caught there and that's where the beadsoftware article recommended putting it.  I'm going to keep looking into it.  I just wanted to publish this code so anyone who's interested can use it.  Enjoy!</p>
  
  <pre><code>using System.Threading;
  using System.Windows.Forms;
  
  public class WebProcessor
  {
      private string GeneratedSource{ get; set; }
      private string URL { get; set; }
  
      public string GetGeneratedHTML(string url)
      {
          URL = url;
  
          Thread t = new Thread(new ThreadStart(WebBrowserThread));
          t.SetApartmentState(ApartmentState.STA);
          t.Start();
          t.Join();
  
          return GeneratedSource;
      }
  
      private void WebBrowserThread()
      {
          WebBrowser wb = new WebBrowser();
          wb.Navigate(URL);
  
          wb.DocumentCompleted += 
              new WebBrowserDocumentCompletedEventHandler(
                  wb_DocumentCompleted);
  
          while (wb.ReadyState != WebBrowserReadyState.Complete)
              Application.DoEvents();
  
          //Added this line, because the final HTML takes a while to show up
          GeneratedSource= wb.Document.Body.InnerHtml;
  
          wb.Dispose();
      }
  
      private void wb_DocumentCompleted(object sender, 
          WebBrowserDocumentCompletedEventArgs e)
      {
          WebBrowser wb = (WebBrowser)sender;
          GeneratedSource= wb.Document.Body.InnerHtml;
      }
  }
  </code></pre>