设置控件的值
/// <summary> /// 根据ID,NAME双重判断并设置值 /// </summary> /// <param name="tagName"></param> /// <param name="id"></param> /// <param name="value"></param> private void SetTxt(string tagName, string name, string id, string value) { HtmlDocument docx = <strong><font color="#FF0000">webBrowser</font></strong>1.Document; foreach (HtmlElement item in docx.GetElementsByTagName(tagName)) {
if (item.GetAttribute("name") != null && item.GetAttribute("name") == name || item.Id == id) { try { item.Focus(); item.SetAttribute("value", value); } catch { item.SetAttribute("value", value); }
} } }
返回指定WebBrowser中图片<IMG></IMG>中的图内容 需要引用MsHtml
/// <summary> /// 返回指定WebBrowser中图片<IMG></IMG>中的图内容 /// </summary> /// <param name="WebCtl">WebBrowser控件</param> /// <param name="ImgeTag">IMG元素</param> /// <returns>IMG对象</returns> private Image GetWebImage(WebBrowser WebCtl, HtmlElement ImgeTag) { HTMLDocument doc = (HTMLDocument)WebCtl.Document.DomDocument; HTMLBody body = (HTMLBody)doc.body; IHTMLControlRange rang = (IHTMLControlRange)body.createControlRange(); IHTMLControlElement Img = (IHTMLControlElement)ImgeTag.DomElement; //图片地址 Image oldImage = Clipboard.GetImage(); rang.add(Img); rang.execCommand("Copy", false, null); //拷贝到内存 Image numImage = Clipboard.GetImage(); try { Clipboard.SetImage(oldImage); } catch { } return numImage; }
一个通用webbrowser类,封装常用方法
public partial class htmlElement { //根据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; }
// 据Type获取元 ,在没有NAME和ID的情况下使用 public HtmlElement GetElement_Type(WebBrowser wb, string type) { HtmlElement e = null; HtmlElementCollection elements = wb.Document.GetElementsByTagName("input"); foreach (HtmlElement element in elements) { if (element.GetAttribute("type") == type) { e = element; } } return e; } // 据Type获取元 ,在没有NAME和ID的情况下使用,并指定是同类type的第 个,GetElement_Type()升级版 public HtmlElement GetElement_Type_No(WebBrowser wb, string type, int i) { int j = 1; HtmlElement e = null; HtmlElementCollection elements = wb.Document.GetElementsByTagName("input"); foreach (HtmlElement element in elements) { if (element.GetAttribute("type") == type) { if (j == i) { e = element; } j++; } } 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); } }
多线程执行webbrowser的方法: webbrowser 只支持STA模式
private void ThreadWebBrowser(string url) { Thread tread = new Thread(new ParameterizedThreadStart(BeginCatch)); tread.SetApartmentState(ApartmentState.STA);//注意创建的线程单元 tread.Start(url); }
private void BeginCatch(object obj) { string url = obj.ToString(); WebBrowser wb = new WebBrowser(); wb.ScriptErrorsSuppressed = true; //在这里Navigate一个空白页面 必须 wb.Navigate("about:blank"); string htmlcode = "";//这里赋值 HTML字符串数据 wb.Document.Write(htmlcode); //执行其他操作 }
|
请发表评论