Use of the WebBrowser control

Source: Internet
Author: User

Description: There is more than one way to implement thread synchronization. In this case, the event was used, and the thread was aborted in the event handler (mainly to answer a friend's question in the CSDN forum). Built a QQ technology Group C #,. NET Technology Exchange Group 242497960 Welcome everyone to join the group "C #, click Link. NET Technology Exchange Group ": Http://jq.qq.com/?_wv=1027&k=ON45Ii0, Common methods

Navigate (string urlstring): Browse URLString-represented URLs
Navigate (system.uri URL): Browse URLs represented by URL
Navigate (string urlstring, string targetframename, byte[] postdata, string additionalheaders): Browse urlstring represented URLs and send messages in PostData
// (usually when we log in to a website, we send the username and password as postdata.)
GoBack (): Back
GoForward (): Forward
Refresh (): Refreshing
Stop (): Stop
GoHome (): Browse Home
Common properties of the WebBrowser control:
Document: Get the documents currently being browsed
Documenttitle: Gets the title of the page currently being browsed
StatusText: Gets the text of the current status bar
URL: Gets the URI of the URL currently being browsed
ReadyState: Get the status of the browse
Common events for WebBrowser controls:
Documenttitlechanged,
Cangobackchanged,
Cangoforwardchanged,
Documenttitlechanged,
ProgressChanged,
ProgressChanged

1. Gets the value of the non-input control:

webbrowser1.document.all[" control ID"]. InnerText; or webBrowser1.Document.GetElementById (" control ID"). InnerText; or webBrowser1.Document.GetElementById (" control ID"). GetAttribute ("value");


2. Gets the value of the input control:

webbrowser1.document.all[" control ID"]. GetAttribute ("value");; or webBrowser1.Document.GetElementById (" control ID"). GetAttribute ("value");

3. Assign values to the input box:

// Input Box " myname "  "123456"; WebBrowser1.Document.GetElementById ("  Password"). SetAttribute ("value""Welcome123");

4, pull down, check, multi-select:

// Drop- down box:secret. SetAttribute ("value""question1");   // check box rememberme. SetAttribute ("Checked""True"); // multiple marquee cookietime. SetAttribute ("checked""checked");


5. Manipulate elements without IDs based on elements known to have an ID:

HtmlElement btndelete = webBrowser1.Document.GetElementById (Passengerid). parent.parent.parent.parent.firstchild.firstchild.children[1]. Firstchild.firstchild;

Depending on the parent,firstchild,children[1] array, the number of levels of elements can be found.

6. Get the style of div or other elements:

WebBrowser1.Document.GetElementById ("adddiv"). Style;

7, directly execute the script function in the page, with dynamic parameters or without parameters are the line:

New object[1];objarray[0] = (Object)this. Labflightnumber.text; WebBrowser1.Document.InvokeScript ("ticketbook", Objarray); WebBrowser1.Document.InvokeScript ("return False");

8, Automatic Click, automatic submission:

HtmlElement Btnadd = doc. getElementById ("adddiv"). Firstchild;btnadd.invokemember ("click");

9, automatic assignment, and then click the Submit button when the script error or the problem is always loaded, the Click event is usually executed too fast, you need to delay the execution of the Submit button event with the help of the Timer control:

 This true ;  This  + 2 ; Private void Timer1_Tick (object  sender, EventArgs e) {    thisfalse ;    Clickbtn.invokemember ("click"); // Perform a button action }


10. Masking script Error:

Set the WebBrowser control scripterrorssuppressed to True


11, automatically click the pop-up prompt box:

Private void webbrowser1_navigated (object  sender, WebBrowserNavigatedEventArgs e) {  //  Auto-click Pop-Up confirmation or pop-up hint  IHTMLDocument2 vdocument = (IHTMLDocument2) webBrowser1.Document.DomDocument;  VDocument.parentWindow.execScript ("" "JavaScript " // Popup Confirmation  VDocument.parentWindow.execScript ("" "javaScript" ); // Popup Prompt }

WebBrowser when the page is loaded, auto-click (block) of the popup box when you do some automation in the page

Private void webbrowser1_documentcompleted (object  sender, WebBrowserDocumentCompletedEventArgs e) {     // Auto-click Popup Confirmation or pop-up prompt    IHTMLDocument2 vdocument = (IHTMLDocument2) webBrowser1.Document.DomDocument;    VDocument.parentWindow.execScript ("" "JavaScript " // Popup Confirmation    VDocument.parentWindow.execScript ("" "javaScript" ); // pop-    up tips // Here's your code of execution: }

12. Get the IFRAME in the Web page and set the src of the IFRAME

HTMLDocument docframe = webbrowser1.document.window.frames["mainFrame"= webbrowser1.document.all.frames["mainFrame"]. Document; docframe.all["mainFrame"]. SetAttribute ("src""http://www.baidu.com/");

13, the existence of the IFRAME in the Web page Webbrowser1.url and webbrowser1_documentcompleted in the E.url is not the same, the former is the main frame URL, the latter is the current active box port URL.


14. Let the control focus

 This . Webbrowser1.select ();  This . Webbrowser1.focus ();d OC. all["tpl_password_1"]. Focus ();


15. Open the local Web file

@" \test.html ");

16. Get elements, forms

Press CTRL + C to copy code//Get element public htmlelement getelement_name (WebBrowser wb,string name) {HtmlElement e = WB according to name. Document.all[name]; return e;} Gets the element public htmlelement getelement_id (WebBrowser WB, string ID) based on the id {htmlelement e = wb. document.getElementById (ID); return e;} Gets the element public htmlelement getelement_index (WebBrowser wb,int index) {htmlelement e = WB according to index. Document.all[index]; return e;} Get form table sole name name, return form public htmlelement getelement_form (WebBrowser wb,string form_name) {htmlelement e = wb. Document.forms[form_name]; return e;} Set the value of the element Value property public void Write_value (HtmlElement e,string value) {E.setattribute ("value", value);} Methods of executing elements, such as: Click,submit (requires form table sole name), public void Btn_click (HtmlElement e,string s) {e.invokemember (s);} Description: There is more than one way to implement thread synchronization. In this case, the event was used, and the thread was aborted in the event handler (mainly to answer a friend's question in the CSDN forum). Built a QQ technology Group C #,. NET Technology Exchange Group 242497960   Welcome to join the group "C #, click on the link. NET Technology Exchange Group ":http://jq.qq.com/?_wv=1027&k=on45ii 

This article is from the "9412405" blog, please be sure to keep this source http://9422405.blog.51cto.com/9412405/1557329

Use of the WebBrowser control

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.