Use of the webbrowser control in C #

Source: Internet
Author: User

Keywords:C # Webbrowser

Author: txw1958

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

 

0. Common Methods

Navigate (string urlstring): browse URL navigate (system. uri URL): the URL navigate (string urlstring, string targetframename, byte [] postdata, string additionalheaders): the URL indicated by urlstring, and send the message in postdata // (usually we will send the user name and password as postdata when logging on to a website) Goback (): backward goforward (): Forward refresh (): refresh stop (): Stop Gohome (): common property of the webbrowser control on the browser page: Document: Get the currently viewed document documenttitle: Get the currently viewed webpage title statustext: get the text URL of the Current Status Bar: Get the urireadystate of the URL currently being browsed: Get the browsing status common events of the webbrowser control: documenttitlechanged, cangobackchanged, cangoforwardchanged, documenttitlechanged, progresschanged, progresschanged

 

1. Obtain the value of a 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. Obtain the value of the input control:

Webbrowser1.document. All ["Control ID"]. getattribute ("value"); or webbrowser1.document. getelementbyid ("Control ID"). getattribute ("value ");

 

3. assign a value to the input box:

// Input box user. innertext = "myname"; password. innertext = "123456"; webbrowser1.document. getelementbyid ("password"). setattribute ("value", "welcome123 ");

 

4. drop-down, check, and multiple options:

// Drop-down box: secret. setattribute ("value", "question1"); // check box rememberme. setattribute ("checked", "true"); // multiple choice box cookietime. setattribute ("checked", "checked ");

5. Operate the element without id based on the known element with ID:

HtmlElement btnDelete = webBrowser1.Document.GetElementById(passengerId).Parent.Parent.Parent.Parent.FirstChild.FirstChild.Children[1].FirstChild.FirstChild;

Based on the parent, firstchild, children [1] array, you can find the level of elements.

 

6. Get the style of the Div or other elements:

webBrowser1.Document.GetElementById("addDiv").Style;

 

7. directly execute the Script Function on the page, with dynamic parameters or without parameters:

Object[] objArray = new Object[1];objArray[0] = (Object)this.labFlightNumber.Text;webBrowser1.Document.InvokeScript("ticketbook", objArray);webBrowser1.Document.InvokeScript("return false");

 

8. Automatic clicking and automatic submission:

HtmlElement btnAdd = doc.GetElementById("addDiv").FirstChild;btnAdd.InvokeMember("Click");

 

9. automatically assign values. If a script error or loading problem occurs when you click the submit button, the event is executed too quickly. In this case, you need to use the timer control to delay the task of executing the submit button:

This. timer1.enabled = true; this. timer1.interval = 1000*2; private void timer1_tick (Object sender, eventargs e) {This. timer1.enabled = false; clickbtn. invokemember ("click"); // execute the button operation}

 
10. Script blocking error:

Set the webbrowser control scripterrorssuppressed to true.

 
11. Automatically click the pop-up prompt box:

Private void webbrowserinclunavigated (Object sender, webbrowsernavigatedeventargs e) {// click the pop-up confirmation or the prompt ihtmldocument2 vdocument = (ihtmldocument2) webbrowser1.document appears. domdocument; vdocument.parentwindow.exe cscript ("function confirm (STR) {return true;}", "JavaScript"); // the pop-up confirmation vdocument.parentwindow.exe cscript ("function alert (STR) {return true;} "," JavaScript "); // a prompt is displayed}

After the webbrowser page is loaded, automatic clicking (blocking) in the pop-up box is displayed when some automatic operations are performed on the page)

Private void webbrowserincludocumentcompleted (Object sender, webbrowserdocumentcompletedeventargs e) {// click the pop-up confirmation or the prompt ihtmldocument2 vdocument = (ihtmldocument2) webbrowser1.document appears. domdocument; vdocument.parentwindow.exe cscript ("function confirm (STR) {return true;}", "JavaScript"); // the pop-up confirmation vdocument.parentwindow.exe cscript ("function alert (STR) {return true;} "," JavaScript "); // a prompt is displayed. // your Execution Code is shown below}

 

12. Get the IFRAME In the webpage and set the SRC of 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. When an IFRAME exists in a webpage, webbrowser1.url is different from E. url in webbrowser1_documentcompleted. The former is the URL of the main frame, and the latter is the URL of the current active frame port.

14. Focus controls

this.webBrowser1.Select();this.webBrowser1.Focus();doc.All["TPL_password_1"].Focus();

 
15. Open a local webpage File

webBrowser1.Navigate(Application.StartupPath + @"\Test.html");

 

16. Obtain elements and forms

// Obtain the public htmlelement getelement_name (webbrowser WB, string name) {htmlelement E = WB according to name. document. all [name]; Return e;} // obtain the public htmlelement getelement_id (webbrowser WB, string ID) {htmlelement E = WB. document. getelementbyid (ID); Return e;} // obtain the public htmlelement getelement_index (webbrowser WB, int index) {htmlelement E = WB. document. all [Index]; Return e;} // get form 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 attribute public void write_value (htmlelement E, string value) {e. setattribute ("value", value);} // method of executing elements, such as: Click, submit (form name required) and other public void btn_click (htmlelement E, string S) {e. invokemember (s );}

 

 

Related Article

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.