WPF WebBrowser Control and the Document Object

November 12 2008

If you use a Frame control and navigate to a URI in WPF 3.5 SP1, you'll end up with instantiating the WebBrowser control under the hood, which is new in WPF 3.5 SP1. In order to get at it, you’ll need to cast the Content property of the Frame to a WebBrowser.  I recently had a situation where I needed to actually get at the DOM itself from the WebBrowser control However, I was initially mystified when I saw that the Document property returned a raw .NET object. What do I do with that?

What you need to do is add a reference to the COM type Microsoft HTML Object Library from Visual Studio. That will create a managed wrapper for the MSHTML.dll.  Then you can cast the object returned by the document property to a mshtml.HTMLDocumentClass.  Then you have full access to the DOM!

Oh, one other gotcha: make sure you don’t do this right after calling Navigate() on your frame, because  Navigate() is asynch. You need to do this after the frame’s ContentRendered event has fired.  And, because ContentRendered fires as soon as the Frame is loaded, it may not yet contain the WebBrowser control, so you need to do some type checking. Here’s a code snippet:

    void moddedFrame_ContentRendered(object sender, EventArgs e)
    {
        if (moddedFrame.Content.GetType() == typeof(WebBrowser))
        {
            WebBrowser webBrowser = (WebBrowser)moddedFrame.Content;
            mshtml.HTMLDocumentClass dom = (mshtml.HTMLDocumentClass)webBrowser.Document;
            Console.Write(dom.body.innerHTML);
        }
    }

Comments (15) -

11/8/2008 2:07:00 AM #

xiaopeng wang

Hi,
I can't get the dom and it was null: this is my codes

http://www.freep.cn/Default.aspx" target="_blank" >http://www.freep.cn/Default.aspx
          

xiaopeng wang

11/8/2008 7:16:00 AM #

xiaopeng wang

Codes is here:
freep.cn/p.aspx?u=v20__p_0811131015083250_0.jpg" target="_blank" >freep.cn/p.aspx?u=v20__p_0811131015083250_0.jpg
          

xiaopeng wang

11/8/2008 3:58:00 PM #

Would be nice if stuff like this was in the documentation in addition to your illustrious blog... Thanks for posting! Good information.
          

GeekTieGuy

11/9/2008 6:13:00 AM #

Walter

currently Webbrowser doesn't work well when I set it to allowtransparency = true.

is there any plan for the product team to fix it?
          

Walter

11/10/2008 5:59:00 PM #

Glenn

re: WebBrowser disappears when allowtransparency=true:  we've gone thru our contacts at MS and "there are no firm plans to fix this, as deeply depends on complex legacy restrictions in technology".  It has to do with the way wpf renders windows vs the underlying webbrowser technology that uses win32 painting.
          

Glenn

11/14/2008 10:17:00 AM #

Walter

currently we are building a desktop gadget with WPF, unfortunately we need to change the storyboard because of this "bug". If product team able to fix it, it definately will be very COOL!
          

Walter

7/22/2011 11:20:15 PM #

cheap nike air max uk

Thanks for taking the time to discuss this, I feel strongly about it and love learning more on this topic. If possible, as you gain expertise, would you mind updating your blog with more information? It is extremely helpful for me.

cheap nike air max uk

8/25/2011 7:54:43 PM #

allergy relief

Thanks for the post, I'm currently writing a WPF program dealing with the web and need to access elements on the page. This will be very helpful for doing that.

allergy relief

8/25/2011 7:55:16 PM #

allergy relief

Thanks for the post, I'm currently writing a WPF program dealing with the web and need to access elements on the page. This will be very helpful for doing that.

allergy relief

8/25/2011 7:55:50 PM #

allergy relief

Thanks for the post, I'm currently writing a WPF program dealing with the web and need to access elements on the page. This will be very helpful for doing that.

allergy relief

8/25/2011 7:56:24 PM #

allergy relief

Thanks for the post, I'm currently writing a WPF program dealing with the web and need to access elements on the page. This will be very helpful for doing that.

allergy relief

10/3/2011 6:35:38 AM #

msrsshahat

s

msrsshahat

10/3/2011 6:36:07 AM #

msrsshahat

s

msrsshahat

10/3/2011 6:36:24 AM #

msrsshahat

s

msrsshahat

10/3/2011 6:36:49 AM #

msrsshahat

s

msrsshahat

Add comment

Enter your name, handle, alias, or email.

We'll incarnate your avatar from the services below.



biuquote
Loading