public IEElement(Browser browser, MSHTML.IHTMLElement Element) { Browser = browser; RawElement = Element; ClassName = Element.className; Id = Element.id; TagName = Element.tagName.ToLower(); Name = ""; try { if (!(RawElement.getAttribute("Name") is System.DBNull)) { Name = (string)RawElement.getAttribute("Name"); } } catch (Exception) { } if (TagName == "option") { var option = Element as MSHTML.IHTMLOptionElement; Name = option.text; } if (TagName == "input") { MSHTML.IHTMLInputElement inputelement = Element as MSHTML.IHTMLInputElement; Type = inputelement.type.ToLower(); } try { MSHTML.IHTMLUniqueName id = RawElement as MSHTML.IHTMLUniqueName; UniqueID = id.uniqueID; } catch (Exception) { } IndexInParent = -1; if (Element.parentElement != null && !string.IsNullOrEmpty(UniqueID)) { MSHTML.IHTMLElementCollection children = (MSHTML.IHTMLElementCollection)Element.parentElement.children; for (int i = 0; i < children.length; i++) { if (children.item(i) is MSHTML.IHTMLUniqueName id && id.uniqueID == UniqueID) { IndexInParent = i; break; } } } }
void ContentClick(object sender, RoutedEventArgs e) { Debug.WriteLine("On Content Click."); SHDocVw.ShellWindows shellWindows = new SHDocVw.ShellWindows(); //遍历所有选项卡 foreach (SHDocVw.InternetExplorer Browser in shellWindows) { if (Browser.LocationURL.Contains("www.baidu.com")) { MSHTML.IHTMLDocument2 doc2 = (MSHTML.IHTMLDocument2)Browser.Document; MSHTML.IHTMLElementCollection inputs = (MSHTML.IHTMLElementCollection)doc2.all.tags("INPUT"); MSHTML.HTMLInputElement input1 = (MSHTML.HTMLInputElement)inputs.item("kw", 0); input1.value = "刘德华"; MSHTML.IHTMLElement element2 = (MSHTML.IHTMLElement)inputs.item("su", 0); element2.click(); } } }