C#WebBrowser控件使用教程与技巧收集
生活随笔
收集整理的這篇文章主要介紹了
C#WebBrowser控件使用教程与技巧收集
小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
?常用的方法
?
1 Navigate(string urlString):瀏覽urlString表示的網(wǎng)址 2 Navigate(System.Uri url):瀏覽url表示的網(wǎng)址 3 Navigate(string urlString, string targetFrameName, byte[] postData, string additionalHeaders): 瀏覽urlString表示的網(wǎng)址,并發(fā)送postData中的消息 4 //(通常我們登錄一個(gè)網(wǎng)站的時(shí)候就會(huì)把用戶名和密碼作為postData發(fā)送出去) 5 GoBack():后退 6 GoForward():前進(jìn) 7 Refresh():刷新 8 Stop():停止 9 GoHome():瀏覽主頁 10 WebBrowser控件的常用屬性: 11 Document:獲取當(dāng)前正在瀏覽的文檔 12 DocumentTitle:獲取當(dāng)前正在瀏覽的網(wǎng)頁標(biāo)題 13 StatusText:獲取當(dāng)前狀態(tài)欄的文本 14 Url:獲取當(dāng)前正在瀏覽的網(wǎng)址的Uri 15 ReadyState:獲取瀏覽的狀態(tài) 16 WebBrowser控件的常用事件: 17 DocumentTitleChanged, 18 CanGoBackChanged, 19 CanGoForwardChanged, 20 DocumentTitleChanged, 21 ProgressChanged, 22 ProgressChanged 23 DocumentCompleted 頁面加載完成之后的事件?
獲取非input控件的值
?
1 webBrowser1.Document.All["控件ID"].InnerText; 2 或webBrowser1.Document.GetElementById("控件ID").InnerText; 3 或webBrowser1.Document.GetElementById("控件ID").GetAttribute("value");?
屏蔽腳本錯(cuò)誤: 將WebBrowser控件ScriptErrorsSuppressed設(shè)置為True即可?
?
獲取元素、表單
1 //根據(jù)Name獲取元素 2 public HtmlElement GetElement_Name(WebBrowser wb,string Name) 3 { 4 HtmlElement e = wb.Document.All[Name]; 5 return e; 6 } 7 8 //根據(jù)Id獲取元素 9 public HtmlElement GetElement_Id(WebBrowser wb, string id) 10 { 11 HtmlElement e = wb.Document.GetElementById(id); 12 return e; 13 } 14 15 //根據(jù)Index獲取元素 16 public HtmlElement GetElement_Index(WebBrowser wb,int index) 17 { 18 HtmlElement e = wb.Document.All[index]; 19 return e; 20 } 21 22 //獲取form表單名name,返回表單 23 public HtmlElement GetElement_Form(WebBrowser wb,string form_name) 24 { 25 HtmlElement e = wb.Document.Forms[form_name]; 26 return e; 27 } 28 29 30 //設(shè)置元素value屬性的值 31 public void Write_value(HtmlElement e,string value) 32 { 33 e.SetAttribute("value", value); 34 } 35 36 //執(zhí)行元素的方法,如:click,submit(需Form表單名)等 37 public void Btn_click(HtmlElement e,string s) 38 { 39 40 e.InvokeMember(s); 41 }?
?
1 [DllImport("wininet.dll", CharSet = CharSet.Auto, SetLastError = true)] 2 static extern bool InternetGetCookieEx(string pchUrl, string pchCookieName, StringBuilder pchCookieData, ref System.UInt32 pcchCookieData, int dwFlags, IntPtr lpReserved); 3 private static string GetCookieString(string url) 4 { 5 uint datasize = 1024; 6 StringBuilder cookieData = new StringBuilder((int)datasize); 7 if (!InternetGetCookieEx(url, null, cookieData, ref datasize, 0x2000, IntPtr.Zero)) 8 { 9 if (datasize < 0) 10 return null; 11 cookieData = new StringBuilder((int)datasize); 12 if (!InternetGetCookieEx(url, null, cookieData, ref datasize, 0x00002000, IntPtr.Zero)) 13 return null; 14 } 15 return cookieData.ToString(); 16 } 17 private void webBrowser1_DocumentCompleted_1(object sender, WebBrowserDocumentCompletedEventArgs e) 18 { 19 richTextBox1.Text = string.Empty; 20 if (cbcookie.Checked) 21 { 22 if (checkBox1.Checked) 23 { 24 richTextBox1.Text = GetCookieString(textBox1.Text.Trim()); 25 } 26 else 27 { 28 richTextBox1.Text = webBrowser1.Document.Cookie; 29 } 30 } 31 } 32?
總結(jié)
以上是生活随笔為你收集整理的C#WebBrowser控件使用教程与技巧收集的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: windows和centos进行文件上传
- 下一篇: Mongodb账号密码模式的基本认证