WPF uses WebBrowser to manipulate the main code of a webpage

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.