In WPF, operate source code in webbrowser

Source: Internet
Author: User

Since the WPF webbrowser control is very different from the winform webbrowser control, it was tossed around for a night, and now some keyCodeSummarize.

The Microsoft. mshtml. dll address must be c: \ Program Files \ Microsoft. NET \ primary InterOP assemblies.

 

1. Obtain the element value based on the element ID.

For example, to obtain the value of the src attribute in this label:

Mshtml. ihtmldocument2 doc2 = (mshtml. ihtmldocument2) webbrowser1.document;
Mshtml. ihtmlelement IMG = (mshtml. ihtmlelement) doc2.all. Item ("regimg", 0 );

String imgurl = (string) IMG. getattribute ("src ");

 

2. Fill in the form and confirm

Mshtml. ihtmlelement loginname = (mshtml. ihtmlelement) doc2.all. Item ("loginname", 0 );
Mshtml. ihtmlelement loginpw = (mshtml. ihtmlelement) doc2.all. Item ("password", 0 );
Mshtml. ihtmlelement loginbt = (mshtml. ihtmlelement) doc2.all. Item ("formsubmit", 0 );
Mshtml. ihtmlelement loginyz = (mshtml. ihtmlelement) doc2.all. Item ("regcode", 0 );
Loginname. setattribute ("value", tbloginname. Text );
Loginpw. setattribute ("value", tbloginpassword. Password );
Loginyz. setattribute ("value", tbyz. Text );
Loginbt. Click ();

 

3. Get source code

Textbox1.text = doc2.body. innerhtml;

 

4. Execute JS

Mshtml. ihtmlwindow2 win = (mshtml. ihtmlwindow2) doc2.parentwindow;
Win.exe cscript ("changeregimg ()", "JavaScript"); // use JS

 

5. Disable Js. Currently, the only applicable method found in WPF is:

Public void hidescripterrors (webbrowser WB, bool hide)
{

Fieldinfo ficomwebbrowser = typeof (webbrowser). getfield ("_ axiwebbrowser2", bindingflags. instance | bindingflags. nonpublic );

If (ficomwebbrowser = NULL) return;

Object objcomwebbrowser = ficomwebbrowser. getvalue (WB );

If (objcomwebbrowser = NULL) return;

Objcomwebbrowser. GetType (). invokemember (

"Silent", bindingflags. setproperty, null, objcomwebbrowser, new object [] {hide });

}

Void webbrowser1_navigated (Object sender, navigationeventargs E)
{

Hidescripterrors (webbrowser1,

True );

}

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.