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
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. TheWebBrowserControlis 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 yourWebBrowserControlwould also help us diagnose the problem.I think you want something along the lines of:
Your conversion from
IHTMLDocumenttoHtmlDocumentis invalid.Here’s the IHTMLDocument2 property and method docs.
Expanding on that, in the above example,
webBrowser1.Documentis aSystem.Windows.Forms.HtmlDocument. ItsDomDocumentproperty 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 managedDocumentproperty of the browser control.