【NET】WebBrowser执行脚本以及一般操作代码

 1 public class WebBrowserAssistant
 2     {
 3         System.Windows.Forms.WebBrowser wb;
 4         public WebBrowserAssistant(System.Windows.Forms.WebBrowser wb) {
 5             this.wb = wb;
 6         }
 7         public bool SetElementValue(string id,string value) {
 8             this.wb.Document.GetElementById(id).SetAttribute("value", value);
 9             return true;
10         }
11 
12         public object InvokeScript(string scriptName) {
13             return this.wb.Document.InvokeScript(scriptName);
14         }
15         public object InvokeScript(string scriptName,object[] ps)
16         {
17             return this.wb.Document.InvokeScript(scriptName, ps);
18         }
19         public void GetFocus(string id)
20         {
21             this.wb.Document.GetElementById(id).Focus();
22         }
23         public void GetBlur(string id)
24         {
25             this.wb.Document.GetElementById(id).RemoveFocus();
26         }
27         public string GetInputIDByName(string name)
28         {
29             return GetInputIDByName(name, name);
30         }
31 
32         public void ClickElement(string id) {
33             this.wb.Document.GetElementById(id).InvokeMember("click");
34         }
35         public string GetInputIDByName(string name,string id)
36         {
37             System.Windows.Forms.HtmlElementCollection es = this.wb.Document.GetElementsByTagName("input");
38             if (es != null && es.Count > 0) {
39                 System.Collections.IEnumerator ie = es.GetEnumerator();
40                 System.Windows.Forms.HtmlElement e;
41                 while (ie.MoveNext()) {
42                     e = (System.Windows.Forms.HtmlElement)ie.Current;
43                     if (e.Name.Equals(name))
44                     {
45                         if (string.IsNullOrEmpty(e.Id))
46                         {
47                             e.Id = id;
48                         }
49                         return e.Id;
50                     }
51                 }
52             }
53             return null;
54         }
55 
56         public List<string> GetEletemtByName(string name)
57         {
58             System.Windows.Forms.HtmlElementCollection es = this.wb.Document.GetElementsByTagName("input");
59             if (es != null && es.Count > 0)
60             {
61                 List<string> result = new List<string>();
62                 System.Collections.IEnumerator ie = es.GetEnumerator();
63                 System.Windows.Forms.HtmlElement e;
64                 while (ie.MoveNext())
65                 {
66                     e = (System.Windows.Forms.HtmlElement)ie.Current;
67                     if (e.GetAttribute("type").Equals("checkbox") && e.Name.Equals(name) && e.GetAttribute("checked").Equals("True"))
68                     {
69                         result.Add(e.GetAttribute("value"));
70                     }
71                 }
72                 return result;
73             }
74             return null;
75         }
76         public string ExeScript(string scriptBlock) {
77             return ExeScript(scriptBlock, "tempEletemtTagNameAndID");
78         }
79         public string ExeScript(string scriptBlock,string tempEletemtTagNameAndID) {
80             System.Windows.Forms.HtmlElement ele;
81             if (this.wb.Document.GetElementById(tempEletemtTagNameAndID) == null) {
82                 ele = this.wb.Document.CreateElement(tempEletemtTagNameAndID);
83                 ele.Id = tempEletemtTagNameAndID;
84                 this.wb.Document.Body.AppendChild(ele);
85             }
86             ele = this.wb.Document.CreateElement("script");
87             ele.SetAttribute("type", "text/javascript");
88 
89             ele.SetAttribute("text", string.Format("$j("#{0}").val({1})", tempEletemtTagNameAndID, scriptBlock));
90             this.wb.Document.Body.AppendChild(ele);
91 
92             return this.wb.Document.GetElementById(tempEletemtTagNameAndID).GetAttribute("value");
93         }
94 
95     }

调用

MessageBox.Show(new Huawei.PortableComputer.Util.WebBrowserAssistant(this.wbTaskList).ExeScript("$j('.listContentTable td :checkbox:checked:first').parent().next().next().next().text()"));
原文地址:https://www.cnblogs.com/yomho/p/3216495.html