Search code examples
.netwinformsweb-controlsawesomium

How to get HtmlDocument from Awesomium.WebControl


I'm doing all this in a .Net winforms application.

I'm used to using the IE WebControl that just has a document property. Pretty simple.

Now I'm using the Awesomium.WebControl and I'm trying to get at the underlying document so I can do some DOM work.

But I can't see how to get at it. Nor can I find any articles on how to do that in Google.

Can someone post some working code that starts with a WebControl and ends with an object that has all the DOM methods please? I don't care what class it is as long as it lets me go thru the dom collections and stuff.

I prefer to do my work in .Net and not in javascript. but as a last resort, I'll take what works.

Thanks!


Solution

  • To access the HTML document in your Awesomium WebControl you can execute a JavaScript getElementsByTagName. This is how we do it:

    Dim html As String = WebControl1.ExecuteJavascriptWithResult("document.getElementsByTagName('html')[0].innerHTML")
    

    For example, you can place this code in a handler for the WebControl.DocumentReady event. Otherwise you need to have a check for WebControl1.IsDocumentReady = True before executing the JavaScript.

    We then use the HTMLAgilityPack to parse this HTML string into a HTMLDocument:

    Dim doc As New HtmlDocument
    doc.LoadHtml(html)
    

    which hopefully allows you to do all of the work you need to!