webBrowser操作HTML,可以模拟点击,提交填写等,网页外挂利器,还可以做爬虫等,微软的webBrowser控件可谓强大。
using mshtml; using System; using System.Collections.Generic; using System.Data; using System.Runtime.InteropServices; using System.Text; using System.Windows.Forms; namespace Insert { /// <summary> /// 关于使用webBrowser操作HTML /// </summary> public class HTMLHelper { #region 取出Cookie [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); /// <summary> /// 取出Cookie,当登录后才能取 /// </summary> /// <param name="url">链接地址</param> /// <returns>cookie值</returns> public static string GetCookies(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(); } #endregion #region 注入cookie [DllImport("wininet.dll", CharSet = CharSet.Auto, SetLastError = true)] public static extern bool InternetSetCookie(string lpszUrlName, string lbszCookieName, string lpszCookieData); /// <summary> /// 注入cookie到浏览器中 /// </summary> /// <param name="path">链接地址</param> /// <param name="cookie">cookie值</param> /// <param name="cookiename">cookie名称</param> public void WebBrowserControl(string path, string cookie, string cookiename = null) { if (cookiename == null) InternetSetCookie(path, "JSESSIONID", cookie); else InternetSetCookie(path, cookiename, cookie); } #endregion #region 设置HTML中指定id元素的值 /// <summary> /// 设置HTML中指定id元素的值 /// </summary> /// <param name="wb">WebBrowser</param> /// <param name="idName">id名称</param> /// <param name="value">设置的值</param> public void SetHtmlElementValue(WebBrowser wb, string idName, string value) { HtmlElement tbUserid = wb.Document.All[idName]; tbUserid.SetAttribute("value", value); } #endregion #region 点击HTML中元素处理事件 /// <summary> /// 点击HTML中元素处理事件 /// </summary> /// <param name="wb">WebBrowser</param> /// <param name="idName">id名称</param> public void ClickElement(WebBrowser wb, string idName) { HtmlElement btnSubmit = wb.Document.All[idName]; btnSubmit.InvokeMember("click"); } #endregion #region 去掉弹出确认框 /// <summary> /// 去掉弹出确认框 引用com +Microsoft HTML Object Library /// </summary> /// <param name="wb">WebBrowser</param> public void RemoveAlert(WebBrowser wb) { IHTMLWindow2 win = (IHTMLWindow2)wb.Document.Window.DomWindow; string s = @"function confirm() {"; s += @"return true;"; s += @"}"; s += @"function alert() {}"; win.execScript(s, "javascript"); } #endregion #region 获取系统上Table的数据(未测) /// <summary> /// 获取系统上Table的数据 /// </summary> /// <param name="wb">WebBrowser</param> /// <param name="dt">DataTable</param> /// <param name="TableIndex">页面上第几个Table</param> /// <returns></returns> public DataTable GetHTMLDataTable(WebBrowser wb, DataTable dt, int TableIndex) { HtmlElementCollection tbs = wb.Document.GetElementsByTagName("table"); if (TableIndex > tbs.Count) return null; int count = 0; foreach (HtmlElement tb in tbs) { if (count < TableIndex - 1) { count++; continue; } HtmlElementCollection trs = tb.GetElementsByTagName("tr"); foreach (HtmlElement tr in trs) { HtmlElementCollection tds = tr.GetElementsByTagName("td"); if (tds.Count > 0) { DataRow dr = dt.NewRow(); for (int i = 0; i < tds.Count; i++) { dr[i] = tds[i].InnerText; } dt.Rows.Add(dr); } } } return dt; } #endregion } }
文章评论