1, reference Mshtml.dll
using mshtml;
2, gets the element attribute value
IHTMLDocument2 doc2=(ihtmldocument) webbrowser1. document;ihtmlelement img= (IHTMLElement) doc2.all.item ("regimg",0); string imgurl= (string) Img.getattribute ("src");
3. Take form controls
IHTMLElement loginname= (IHTMLElement) Doc2.all.item ("loginName",0); HtmlElement LOGINPW= (IHTMLElement) doc2.all.item ("password",0); HtmlElement Loingyzm= (IHTMLElement) doc2.all.item ("regcode",0 ); IHTMLElement loginbt= (IHTMLElement) doc2.all.item ("formsubmit", 0);
4. Fill out form controls
Loginname.setattribute ("value", Tbloginname.text); Loginpw.setattribute ( "value", Tbloginpassword.password); Loginyzm.setattribute ("value ", Tbyz.text);
5, click the button
Loginbt.click ();
6, Execute JS script
Method 1:
Ihtmlwindow win= (IHTMLWindow2) doc2.parentwindows;win.execscript ("alert (' hello! ') ","javascript");
Method 2:
WebBrowser1. Invokescript ("eval","alert (' hello! ') ");
7, Shielding alert, confirm, etc., by redefining the implementation
Private Voie webbrowser1_navigated (object Sender,webbrosernavigatedeventargs e) { IHTMLWindow2 win=(IHTMLWindow2) webbrowser1. Document.Window.DomWindow; string s=@ "window.alert=null; window.onerror=null;window.confirm=null; windows.open=null; Window.showmodaldialg=null; " ; Win.execscript (S,"javascript");}
8, receive JS message
[ComVisible (true)]//This sentence should be added to the class definition before it can communicate with COMPrivate voidwebbrowser1_navigated (objec Sender,webbrowsernavigatedeventargs e) {IHTMLWindow2 win=(IHTMLWindow2) webbrowser1. Document.Window.DomWindow; //Suppose the alert message is sent out to handle strings=@"function alert (str) {window.external.procMessage (str);}"; Win.execscript (s),"JavaScript"); WebBrowser1. ObjectForScripting= This;//specifies that the script message is sent to the current instance processing}//methods for handling script messages Public voidProcmessage (strings) {MessageBox.Show ("script message: "+s);}
WPF uses WebBrowser to manipulate the main code of a Web page