I use
SHDocVw.InternetExplorer ie = new SHDocVw.InternetExplorer()
to manage / automate an instance of Internet Explorer. On some pages, I want to run the JavaScript function ( init() ). It seems the best way to do this is to use the HtmlDocument InvokeScript method, and I have not tried the following:
void ie_DocumentComplete(object pDisp, ref object URL) { System.Windows.Forms.HtmlDocument doc = ie.Document; doc.InvokeScript("init"); }
Which fails because doc is null. It seems I cannot get System.Windows.Forms.HtmlDocument from ie.Document . Also, having tried the above, I also tried:
System.Windows.Forms.HtmlDocument doc2 = (System.Windows.Forms.HtmlDocument)ie.Document;
and
System.Windows.Forms.HtmlDocument doc2 = ie.Document as System.Windows.Forms.HtmlDocument;
Any ideas on how I can get this to work, or an even better way to run scripts on the page?
Thanks!!
EDIT
Another way to run a JavaScript function is:
SHDocVw.InternetExplorer ie = new SHDocVw.InternetExplorer() mshtml.HTMLDocument doc = ie.Document; mshtml.IHTMLWindow2 win = doc.parentWindow as mshtml.IHTMLWindow2; win.execScript("init();", "javascript");
But the line
mshtml.IHTMLWindow2 win = doc.parentWindow as mshtml.IHTMLWindow2;
throws an error that this is an invalid listing ( InvalidCastException ) - although IntelliSense (and MSDN) say that doc.parentWindow is IHTMLWindow2 . Any ideas? (I also made sure that the page was fully loaded before running this code)
c # internet-explorer automation
Evan
source share