1

I am working on a WPF aplication and I want to get the rendered HTML of a website that uses Angular JS technology. Here is what I've tried:

First, I create a WPF Web Browser control:

private WebBrowser webBrowser;

Then I navigate it to my target web page:

var uri = new Uri("http://my-angualrjs-site.com", UriKind.RelativeOrAbsolute);
webBrowser.Navigate(uri);

In the LoadCompleted event handler of my WebBrowser instance I cast the retrieved document to mshtml.HTMLDocument:

var doc = (mshtml.HTMLDocument) webb.Document;

And all I get is the raw HTML of the page, not the HTML processed and rendered by Angular JS from the server. Am I missing something?

Pejman
  • 3,784
  • 4
  • 24
  • 33
  • 1
    Have you tried waiting 30 seconds and then grabbing the HTML? – JMK Feb 11 '15 at 08:39
  • Are you implying that the `LoadCompleted` fires before the actual load completion? – Pejman Feb 11 '15 at 08:41
  • Well it will fire once the HTML etc is down, but then AngularJS only starts doing it's thing at this point. Depending on what API calls etc Angular is doing client side it could take a couple of seconds before you get your final result. This applies to any client side Javascript framework. – JMK Feb 11 '15 at 08:41
  • Well, it doesn't look very elegant, but based on what you say I should keep on checking the doc until I get what I need! Let me try that, but in the meantime I will wait for a more elegant solution. ;) – Pejman Feb 11 '15 at 08:44
  • If you're trying to scrape data etc, you may have more luck using something like Fiddler to figure out what API endpoints AngularJS is actually talking to when you visit the site, and then hitting them up directly? – JMK Feb 11 '15 at 08:47
  • In that case I think I'll be facing some authentication issues. Plus it is a rather small part of my project on which I am really reluctant to spend that amount of time! I think I am starting to accept the waiting solution elegant enough. :D – Pejman Feb 11 '15 at 08:56
  • @Pejman, check [this](http://stackoverflow.com/a/18333982/1768303) and [this](http://stackoverflow.com/a/20934538/1768303). – noseratio Feb 11 '15 at 08:56
  • 1
    @JMK, the waiting solution works like a charm, thank you. – Pejman Feb 11 '15 at 09:14
  • 1
    @Noseratio, I am studying your more elegant solutions, thank you too. – Pejman Feb 11 '15 at 09:15

1 Answers1

1

I had a similar issue with not being able to get an angular page to render properly in the control. After following Noseratios answer it was rendering and working good. Maybe this would help you too: C# webbrowser Ajax call

In short, you need to add features to the control to make the behavior closer to IE.

With his solution you'll be able to use code such as:

SetBrowserFeatureControlKey("FEATURE_AJAX_CONNECTIONEVENTS", fileName, 1);
Community
  • 1
  • 1
PostureOfLearning
  • 3,481
  • 3
  • 27
  • 44