3

am loading a webpage using the webbrowser control, in it there is a form that is generateb by javascript.

when i tried to access th elements in c#, they cannot be found.

the page renders well in a normal browser, but in my winforms app, the form elements are not rendered, i can only see the javascript that generated the form when dubugging

a close solution i found on stack was to cast the webbrowser.document.domdocument to ihtmldocument, but i havent been successful with it,

this is what am trying to do

Dim doc As HtmlDocument = DirectCast(wbMain.Document.DomDocument, MSH.IHTMLDocument)

but i get this warning

Runtime errors might occur when converting 'mshtml.IHTMLDocument' to 'System.Windows.Forms.HtmlDocument'.

and whe i ignore and run, i got this error

Unable to cast COM object of type 'mshtml.HTMLDocumentClass' to class type 'System.Windows.Forms.HtmlDocument'. Instances of types that represent COM components cannot be cast to types that do not represent COM components; however they can be cast to interfaces as long as the underlying COM component supports QueryInterface calls for the IID of the interface.

EDIT A sample javascript

<SCRIPT type=text/javascript>  
        (function(){            
        function runElemGen() {
       var ElemGenForm = [{"label":"Email:","name":"ElemGen_email_field","oldName":"","value":"","ElemGen":"text"},{"label":"","name":"ElemGensubscribe","oldName":"","value":"Submit","ElemGen":"submit"}];
       ElemGenBuildForm({makeDivs: true, arr:ElemGenForm, place:"ElemGen-email-form-"});                
        }
      if (window.addEventListener) {
        window.addEventListener("load", runElemGen, false);
      } else {
        window.attachEvent("onload", runElemGen);
      }
    })();
</SCRIPT>

can anyone with a solution help

Smith
  • 5,765
  • 17
  • 102
  • 161

2 Answers2

3

Edit: The solution below fixes the OP's inability to access the dom for debugging purposes. The OP actually needs to solve the problem of the document not appearing as expected (or at all) in the WebBrowserControl. The WebBrowserControl is a repackaging of the ActiveX control of the same name and so will probably use the currently installed version of Internet Explorer. If possible, I suggest launching IE on the computer in question and attempting to render the same document, including the code used to load the document in your WebBrowserControl would also help us diagnose the problem.

I think you want something along the lines of:

if (webBrowser1.Document != null)
{
    var currentDoc = (MSHTML.IHTMLDocument2)webBrowser1.Document.DomDocument;
    return (currentDoc.lastModified);
}

Your conversion from IHTMLDocument to HtmlDocument is invalid.

Here's the IHTMLDocument2 property and method docs.

Expanding on that, in the above example, webBrowser1.Document is a System.Windows.Forms.HtmlDocument. Its DomDocument property is an unmanaged pointer to the document. It's only needed when you are looking for properties of the document that are not exposed on the managed Document property of the browser control.

marr75
  • 5,666
  • 1
  • 27
  • 41
  • thanks for this, but it dosent still solve my initial problem, i can't find the form in dom – Smith Sep 23 '12 at 19:02
  • Can you show us the code you're actually using to prepare, load, and render your browser window? It's hard to diagnose the problem when we only have the code you're trying to use to debug it. – marr75 Sep 23 '12 at 19:07
0

You did say the generated form is not displaying in your webbrowser control, so I am guessing it is displaying in your normal Internet Explorer session, as I correct? Do you realize that when you run the WebBrowser Control, it runs under the IE 7.0 rendering engine? This is done for compatibility. So, if there is anything in your JS that requires IE version 8.0 or above, it will not work in the webbrowser control unless you specifically change some registry settings (or do a metadata tag insertion) indicating that you want the content to render under the currently installed version of IE, or a particular fixed version of your choosing.

If this is the problem, my answer to the following question will almost certainly solve your problem. You can find it here: Script runs slower in the dotnet WebBrowser control

Let me know how it goes, and if it solves your problem, if it doesn't, I'll work with you until we find a solution.

Community
  • 1
  • 1
Erx_VB.NExT.Coder
  • 4,838
  • 10
  • 56
  • 92
  • i meant that when the page loads, with the above javascript, i can see the form in DOM, how can i access this form – Smith Oct 02 '12 at 16:12