C # WebBrowser controls in a detailed

Source: Internet
Author: User

827969653
0  , Common method Navigate (string  urlstring): Browse urlstring represented by URL Navigate (system.uri URL) : Browse URLs represented by URL navigate (stringstringbyte  additionalheaders): Browse urlstring for 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 (): Refresh Stop (): Stop GoHome () : Browse the Home page WebBrowser control's Common properties: Document: Get the currently browsing documents Documenttitle: Gets the page title currently being browsed StatusText: Gets the text URL of the current status bar: Gets the urireadystate of the URL currently being browsed: Gets the status of the browse WebBrowser common events for the control: documenttitlechanged,cangobackchanged,cangoforwardchanged,documenttitlechanged, Progresschanged,progresschanged1 , 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 a value to the input box://Input BoxUser. InnerText ="myname";p Assword. InnerText="123456"; WebBrowser1.Document.GetElementById ("Password"). SetAttribute ("value","Welcome123"); 4, dropdown, check, multi-select:
//Drop- down box:Secret. SetAttribute ("value","Question1"); //check boxRememberMe. SetAttribute ("Checked","True");//Multi-Select boxCookietime. SetAttribute ("checked","checked");
5 , manipulating elements without IDs based on elements that are known to have IDs:htmlelement btndelete= WebBrowser1.Document.GetElementById (Passengerid). parent.parent.parent.parent.firstchild.firstchild.children[1]. Firstchild.firstchild; according to parent,firstchild,children[1] Array, and how many levels of elements can be found. 6 , get the style of a div or other element:webBrowser1.Document.GetElementById ("Adddiv"). Style; 7 , directly executes the script function in the page, with dynamic parameters or without parameters:object[] Objarray=Newobject[1];objarray[0] = (Object) This. Labflightnumber.text;webbrowser1.document.invokescript ("Ticketbook", Objarray); WebBrowser1.Document.InvokeScript ("return False"); 8 , Auto-click, Auto-submit:HtmlElement btnadd= Doc. getElementById ("Adddiv"). Firstchild;btnadd.invokemember ("Click"); 9, automatically assign a value, and then click the Submit button if there is a script error or a problem that has been loaded, the Click event is usually executed too quickly, and the timer control needs to defer the execution of the Submit button event:
 this . Timer1. Enabled = true   this . Timer1. Interval = 1000  * ; private  void  Timer1_Tick (object   sender, EventArgs e) { this . Timer1.    Enabled = false    click  Span style= "color: #800000;" >"); //  Perform a button action    
Ten , Masking script error:  One , 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}
 A , get the IFRAME in the Web page, and set the SRC HTMLDocument of the iframedocframe= webbrowser1.document.window.frames["MainFrame"]. Document; or HTMLDocument docframe= webbrowser1.document.all.frames["MainFrame"]. Document; docframe.all["MainFrame"]. SetAttribute ("src","http://www.baidu.com/");  -, when there is an IFRAME in the page Webbrowser1.url and webbrowser1_documentcompleted in the E.url, the former is the main frame of the URL, which is the current active box port URL.  -, let the control focus This. Webbrowser1.select (); This. Webbrowser1.focus ();d OC. all["tpl_password_1"]. Focus ();  the , open the local Web page file webbrowser1.navigate (Application.startuppath+@"\test.html");  -, getting 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);}

17. Disable Hyperlinks
There are two kinds of hyperlinks, one is the current window direct, one is open in a new window

Of course the window turns directly:
Set the allownavigation of WebBrowser to False

Open in a new window:
Disable new window open, need to handle WebBrowser NewWindow event

private void Webbrowser1_newwindow (object sender, CancelEventArgs e)
{
E.cancel = true;
}

18. Disable Error script prompt
Set the scripterrorssuppressed of the WebBrowser control to True

19. Disable Right-click menu
Set the iswebbrowsercontextmenuenabled of WebBrowser to False

20. Disable shortcut keys
Set the webbrowsershortcutsenabled of WebBrowser to False

Last Note:
Set WebBrowser's Allowwebbrowserdrop to false, and don't forget it.

C # WebBrowser controls in a detailed

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.