Get 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");
Gets the value of the input control
webbrowser1.document.all[" control ID"]. GetAttribute ("value");; or webBrowser1.Document.GetElementById (" control ID"). GetAttribute ("value");
Assigning a value to an input box
" myname " "123456"; WebBrowser1.Document.GetElementById (" Password"). SetAttribute ("value""Welcome123");
Dropdown, check, multi-select
//Drop- down box:Secret. SetAttribute ("value","Question1"); //check boxRememberMe. SetAttribute ("Checked","True");//Multi-Select boxCookietime. SetAttribute ("checked","checked");
Manipulating an element without an ID based on an element that is known to have an ID
HtmlElement btndelete = webBrowser1.Document.GetElementById (Passengerid). parent.parent.parent.parent.firstchild.firstchild.children[1]. Firstchild.firstchild;
Get the style of a div or other element
WebBrowser1.Document.GetElementById ("adddiv"). Style;
Directly executes script functions in the page, with dynamic parameters or without parameters.
New object[1];objarray[0] = (Object)this. Labflightnumber.text; WebBrowser1.Document.InvokeScript ("ticketbook", Objarray); WebBrowser1.Document.InvokeScript ("return False");
Auto-click, Auto-Submit
HtmlElement Btnadd = doc. getElementById ("adddiv"). Firstchild;btnadd.invokemember ("click");
Automatically assign a value, and then click the Submit button if there is a script error or a problem that has been loaded, it is generally a click event execution too fast, you need to use the Timer control to defer the execution of the Submit button event
This true ; This + 2 ; Private void Timer1_Tick (object sender, EventArgs e) { thisfalse ; Clickbtn.invokemember ("click"); // Perform a button action }
Mask script Error
Set the WebBrowser control scripterrorssuppressed to True
Automatically click the pop-up notification 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}
Gets the iframe in the Web page, and sets the SRC for the IFRAME
HTMLDocument docframe = webbrowser1.document.window.frames["mainFrame"= webbrowser1.document.all.frames["mainFrame"]. Document; docframe.all["mainFrame"]. SetAttribute ("src""http://www.sufeinet.com/ " );
When there is an IFRAME in the page Webbrowser1.url and webbrowser1_documentcompleted in the E.url, the former is the main frame 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 ();
Open a local Web page file
@" \test.html ");
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);}
View CodeGet cookies
[DllImport ("Wininet.dll", CharSet = CharSet.Auto, SetLastError =true)] Static extern BOOLInternetgetcookieex (stringPchurl,stringPchcookiename, StringBuilder Pchcookiedata,refSystem.UInt32 Pcchcookiedata,intdwFlags, IntPtr lpreserved); Private Static stringGetcookiestring (stringURL) { UINTDataSize =1024x768; StringBuilder Cookiedata=NewStringBuilder ((int) datasize); if(! Internetgetcookieex (URL,NULL, Cookiedata,refDataSize,0x2000, IntPtr.Zero)) { if(DataSize <0) return NULL; Cookiedata=NewStringBuilder ((int) datasize); if(! Internetgetcookieex (URL,NULL, Cookiedata,refDataSize,0x00002000, IntPtr.Zero)) return NULL; } returncookiedata.tostring (); } Private voidWebbrowser1_documentcompleted_1 (Objectsender, WebBrowserDocumentCompletedEventArgs e) {richTextBox1.Text=string. Empty; if(Cbcookie. Checked) {if(checkbox1.checked) {richTextBox1.Text=getcookiestring (TextBox1.Text.Trim ()); } Else{richTextBox1.Text=WebBrowser1.Document.Cookie; } } }
View CodeHow to execute code after loading a page
//This event is performed every time the current page is loaded. Private voidWebbrowser1_documentcompleted (Objectsender, WebBrowserDocumentCompletedEventArgs e) { //E.url is the page that is currently loaded, if(E.url.tostring (). Contains ("http://sufeinet.com")) { //Perform action 1 } Else if(E.url.tostring (). Contains ("http://baidu.com")) { //Perform action 2 } }
How to disable opening a Web page in a new window
Private void Webbrowser1_newwindow (object sender, CancelEventArgs e) { string url = ((System.Windows.Forms.WebBrowser) sender). StatusText; WebBrowser1.Navigate (URL); true ; }
How to set Cookies
Webbrowser1.document.cookie= "Your Cookie value";
Basic methods of controls
Navigate (string URLString): Browse the URL navigate (system.uri URL) represented by urlstring: Browse the URL represented by the URLs navigate ( string URLString, string targetframename, byte [] postdata, string Additionalheaders): Browse to the URL represented by urlstring and send the message in 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,progresschangeddocumentcompleted event replication Code after page load is complete
This article originates from: http://www.sufeinet.com/thread-3941-1-1.html.
C#webbrowser controls using tutorials and tips