C#WebBrowser控件使用教程与技巧收集
2016-10-24 05:28
239 查看
常用的方法
获取非input控件的值
获取元素、表单
Navigate(string urlString):浏览urlString表示的网址 Navigate(System.Uri url):浏览url表示的网址 Navigate(string urlString, string targetFrameName, byte[] postData, string additionalHeaders): 浏览urlString表示的网址,并发送postData中的消息 //(通常我们登录一个网站的时候就会把用户名和密码作为postData发送出去) GoBack():后退 GoForward():前进 Refresh():刷新 Stop():停止 GoHome():浏览主页 WebBrowser控件的常用属性: Document:获取当前正在浏览的文档 DocumentTitle:获取当前正在浏览的网页标题 StatusText:获取当前状态栏的文本 Url:获取当前正在浏览的网址的Uri ReadyState:获取浏览的状态 WebBrowser控件的常用事件: DocumentTitleChanged, CanGoBackChanged, CanGoForwardChanged, DocumentTitleChanged, ProgressChanged, ProgressChanged DocumentCompleted 页面加载完成之后的事件
获取非input控件的值
webBrowser1.Document.All["控件ID"].InnerText; 或webBrowser1.Document.GetElementById("控件ID").InnerText; 或webBrowser1.Document.GetElementById("控件ID").GetAttribute("value");
获取元素、表单
//根据Name获取元素 public HtmlElement GetElement_Name(WebBrowser wb,string Name) { HtmlElement e = wb.Document.All[Name]; return e; } //根据Id获取元素 public HtmlElement GetElement_Id(WebBrowser wb, string id) { HtmlElement e = wb.Document.GetElementById(id); return e; } //根据Index获取元素 public HtmlElement GetElement_Index(WebBrowser wb,int index) { HtmlElement e = wb.Document.All[index]; return e; } //获取form表单名name,返回表单 public HtmlElement GetElement_Form(WebBrowser wb,string form_name) { HtmlElement e = wb.Document.Forms[form_name]; return e; } //设置元素value属性的值 public void Write_value(HtmlElement e,string value) { e.SetAttribute("value", value); } //执行元素的方法,如:click,submit(需Form表单名)等 public void Btn_click(HtmlElement e,string s) { e.InvokeMember(s); }
[DllImport("wininet.dll", CharSet = CharSet.Auto, SetLastError = true)] static extern bool InternetGetCookieEx(string pchUrl, string pchCookieName, StringBuilder pchCookieData, ref System.UInt32 pcchCookieData, int dwFlags, IntPtr lpReserved); private static string GetCookieString(string url) { uint datasize = 1024; StringBuilder cookieData = new StringBuilder((int)datasize); if (!InternetGetCookieEx(url, null, cookieData, ref datasize, 0x2000, IntPtr.Zero)) { if (datasize < 0) return null; cookieData = new StringBuilder((int)datasize); if (!InternetGetCookieEx(url, null, cookieData, ref datasize, 0x00002000, IntPtr.Zero)) return null; } return cookieData.ToString(); } private void webBrowser1_DocumentCompleted_1(object sender, WebBrowserDocumentCompletedEventArgs e) { richTextBox1.Text = string.Empty; if (cbcookie.Checked) { if (checkBox1.Checked) { richTextBox1.Text = GetCookieString(textBox1.Text.Trim()); } else { richTextBox1.Text = webBrowser1.Document.Cookie; } } }
相关文章推荐
- C#WebBrowser控件使用教程与技巧收集
- C#WebBrowser控件使用教程与技巧收集
- C#WebBrowser控件使用教程与技巧收集
- C#WebBrowser控件使用教程与技巧收集--苏飞收集
- C#WebBrowser控件使用教程与技巧收集 - 转
- C#WebBrowser控件使用教程与技巧收集--苏飞收集
- C#WebBrowser控件使用教程与技巧收集
- C#WebBrowser控件使用教程与技巧收集
- C#WebBrowser控件使用教程与技巧收集
- C#WebBrowser控件使用教程与技巧收集
- C#WebBrowser控件使用教程与技巧收集
- C#WebBrowser控件使用教程与技巧收集
- C#WebBrowser控件使用教程与技巧收集--苏飞收集
- C#WebBrowser控件使用教程与技巧收集
- C#WebBrowser控件使用教程与技巧收集--苏飞收集
- C#WebBrowser控件使用教程与技巧收集
- C#WebBrowser控件使用教程与技巧
- emWin使用资料收集(一) ----- emWin 2天速成实例教程008_IconView图标控件使用方法
- 关于C#WinForm的datagridview控件的使用经验及技巧收集(持续更新)
- datagrid控件使用技巧大集合