3

I am trying to implement a browser-like little application that would allow me to modify the viewed web-sites appearance (e.g. make the font for links bigger). It is designed for Microsoft Surface, to be used on a huge touchscreen. It uses WPF for the UI.

I am intending to use a WebBrowser control for this task. However there are two classes called WebBrowser in the docs. One of them is in System.Windows.Forms, the other in System.Windows.Controls. The first one gives access to DOM model, but is intended for Forms applications (if I understand correctly, that's definitely not what I have). The second one is added by default if you add the controller in xaml, but it gives no access to the DOM.

So, how do I access the DOM model from a WebBrowser for Surface? I am very new to c# and Microsoft technologies, so I apologise if my question is unclear or obvious.

1 Answer 1

10

For the System.Windows.Controls.WebBrowser class you can use the Document property. Add mshtml reference to your project that should be available by right click on project and selecting Add Reference, then you should be able to cast it to mshtml.IHTMLDocument2

mshtml.IHTMLDocument2 htmlDoc = webBrowser.Document as mshtml.IHTMLDocument2;
// do something like find button and click
htmlDoc.all.item("testBtn").click(); 
5
  • Thanks a lot! A quick question - where I should have found that information? As I couldn't find that anywhere... Oct 19, 2012 at 8:59
  • Would I be able to read dynamic dom ? i mean if the page inside the webbrowser build a html dynamic using ajax , will I be able to read it ?
    – Royi Namir
    Feb 7, 2013 at 9:10
  • 1
    it should be webBrowser.Document.DomDocument
    – Ahmad
    May 23, 2015 at 13:14
  • 2
    @Ahmad this answer is related to Controls.WebBrowser, not Forms.WebBrowser
    – volody
    Aug 5, 2015 at 14:11
  • I have a webbrowser, and i need one next click, it works fine, then on the next form on the same url, i need username/pwd and a login button click. Is it possible to do this with hand from code? Eg. from WebBrowser_LoadCompleted event? First click fine, the ui ok, but navigating not comes in WPF and no events happening... Jul 5, 2023 at 15:16

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.