The use of WebBrowser controls in C #

Source: Internet
Author: User

Original: http://www.cnblogs.com/txw1958/archive/2012/09/24/CSharp-WebBrowser.html

0. Common methods

1Navigate (stringurlstring): Browse urlstring-represented URLs2 Navigate (system.uri URL): Browse URLs represented by URL3Navigate (stringURLString,stringTargetframename,byte[] PostData,stringadditionalheaders): Browse urlstring represented URLs and send messages in PostData4 //(usually when we log in to a website, we send the username and password as postdata.)5 GoBack (): Back6 GoForward (): Forward7 Refresh (): Refreshing8 Stop (): Stop9 gohome (): Browse HomeTen Common properties of the WebBrowser control: One Document: Get the documents currently being browsed A 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 the ReadyState: Get the status of the browse - Common events for WebBrowser controls: - documenttitlechanged, - cangobackchanged, + cangoforwardchanged, - documenttitlechanged, + ProgressChanged, AProgressChanged

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 user. InnerText = "MyName";p assword. InnerText = "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");//multi-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:

object[] Objarray = 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.timer1.Enabled = True;this.timer1.interval = $ * 2;private void Timer1_Tick (object sender, EventArgs e) {This    . timer1. Enabled = false;    Clickbtn.invokemember ("click");//Perform a push-button operation}

10. Masking script Error:

Set the WebBrowser control scripterrorssuppressed to True

11, automatically click the pop-up prompt box:

Private voidWebbrowser1_navigated (Objectsender, WebBrowserNavigatedEventArgs e) {  //Auto-click Popup Confirmation or pop-up promptIHTMLDocument2 vdocument =(IHTMLDocument2) webBrowser1.Document.DomDocument; VDocument.parentWindow.execScript ("function confirm (str) {return true;}","JavaScript");//Popup ConfirmationVDocument.parentWindow.execScript ("function alert (str) {return true;}","JavaScript");//pop-up tips}

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

Private voidWebbrowser1_documentcompleted (Objectsender, WebBrowserDocumentCompletedEventArgs e) {    //Auto-click Popup Confirmation or pop-up promptIHTMLDocument2 vdocument =(IHTMLDocument2) webBrowser1.Document.DomDocument; VDocument.parentWindow.execScript ("function confirm (str) {return true;}","JavaScript");//Popup ConfirmationVDocument.parentWindow.execScript ("function alert (str) {return true;}","JavaScript");//pop-up tips//here is your code of execution action}

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

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

13, the presence of an 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

WebBrowser1.Navigate (Application.startuppath + @ "\test.html");

16. Get elements, forms

//get elements by name PublicHtmlElement Getelement_name (WebBrowser WB,stringName) {HtmlElement e=WB.    Document.all[name]; returne;}//get elements by ID PublicHtmlElement getelement_id (WebBrowser WB,stringID) {HtmlElement e=WB.    document.getElementById (ID); returne;}//get elements based on index PublicHtmlElement Getelement_index (WebBrowser WB,intindex) {HtmlElement e=WB.    Document.all[index]; returne;}//get form table sole name name, return form PublicHtmlElement Getelement_form (WebBrowser WB,stringform_name) {HtmlElement e=WB.    Document.forms[form_name]; returne;}//set the value of the element Value property Public voidWrite_value (HtmlElement E,stringvalue) {E.setattribute ("value", value);}//methods of executing elements, such as: Click,submit (requires form table sole name), etc. Public voidBtn_click (HtmlElement E,strings) {E.invokemember (s);}

The use of WebBrowser controls in C #

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.