private static bool GetEleParentFrames(mshtml.IHTMLDOMNode root, mshtml.IHTMLDOMNode node, List <mshtml.IHTMLDOMNode> frames) { bool flag = false; if (root == node) { return(true); } bool flag2 = false; switch (root.nodeName.ToLower()) { case "frame": case "iframe": flag2 = true; break; } IHTMLDOMChildrenCollection childNodes = null; if (flag2) { SHDocVw.IWebBrowser2 browser = root as SHDocVw.IWebBrowser2; if (browser != null) { mshtml.IHTMLDocument2 document = browser.Document as mshtml.IHTMLDocument2; if (document != null) { mshtml.IHTMLDOMNode parentElement = document.body.parentElement as mshtml.IHTMLDOMNode; childNodes = parentElement.childNodes as IHTMLDOMChildrenCollection; } } } if (childNodes == null) { childNodes = root.childNodes as IHTMLDOMChildrenCollection; } if (childNodes == null) { return(false); } for (int i = 0; i < childNodes.length; i++) { mshtml.IHTMLDOMNode node3 = childNodes.item(i) as mshtml.IHTMLDOMNode; if (GetEleParentFrames(node3, node, frames)) { if (flag2) { frames.Add(root); } flag = true; } } return(flag); }
private static Element getFromCollection(IHTMLDOMChildrenCollection Collection) { Element element = new Element(); if (Collection != null && Collection.length > 0) { element.element = Collection.item(0) as IHTMLElement; element.count = Collection.length; } return(element); }
/// <summary> /// 得到node的所有父亲frames /// </summary> /// <param name="root"></param> /// <param name="elem"></param> /// <param name="frames"></param> /// <returns></returns> private static bool _getEleParentFrames(IHTMLDOMNode root, IHTMLDOMNode node, List <IHTMLDOMNode> frames) { if (root == node) { return(true); } bool isFrame = false; string tag = root.nodeName.ToLower(); if (tag == "frame" || tag == "iframe") { isFrame = true; } IHTMLDOMChildrenCollection cs = null; if (isFrame) { IWebBrowser2 pwb = root as IWebBrowser2; if (pwb != null) { IHTMLDocument2 pdoc2 = pwb.Document as IHTMLDocument2; if (pdoc2 != null) { IHTMLDOMNode htmlElem = pdoc2.body.parentElement as IHTMLDOMNode; cs = htmlElem.childNodes as IHTMLDOMChildrenCollection; } } } if (cs == null) { cs = root.childNodes as IHTMLDOMChildrenCollection; } if (cs == null) { return(false); } for (int idx = 0; idx < cs.length; idx++) { IHTMLDOMNode c = cs.item(idx) as IHTMLDOMNode; if (_getEleParentFrames(c, node, frames)) { if (isFrame) { frames.Add(root); } return(true); } } return(false); }
private void findElement() { HtmlElement body = _browser.Document.Body; //C#网页元素索引(不包含文本节点) int index = 0; //dom元素索引(包含文本节点) int domIndex = 0; IHTMLDOMNode node = null; HtmlElement element = null; Stack <KeyValuePair <HtmlDocument, int> > frameIndex = new Stack <KeyValuePair <HtmlDocument, int> >(); // 保存网页中找到的文本节点 Stack <KeyValuePair <HtmlElement, string> > textNode = new Stack <KeyValuePair <HtmlElement, string> >(body.All.Count); // 保存网页中的form //Stack<HtmlElement> form = new Stack<HtmlElement>(3); // 保存正在遍历的元素列表 Stack <KeyValuePair <HtmlElement, int[]> > enumList = new Stack <KeyValuePair <HtmlElement, int[]> >(body.All.Count); HtmlElement currentElement = body; IHTMLDOMChildrenCollection domCollection = ((IHTMLDOMNode)body.DomElement).childNodes; int currentFrameIndex = 0; for (; domIndex < domCollection.length || enumList.Count > 0; domIndex++, index++) { if (domCollection.length <= domIndex) { KeyValuePair <HtmlElement, int[]> prev = enumList.Pop(); domCollection = ((IHTMLDOMNode)prev.Key.DomElement).childNodes; currentElement = prev.Key; index = prev.Value[0]; domIndex = prev.Value[1]; continue; } node = domCollection.item(domIndex); if (node.nodeType == 3) // 若当前是文本节点则放入textNode中 { string text = HandleFindedText(node.nodeValue); if (string.IsNullOrEmpty(text) == false) { textNode.Push(new KeyValuePair <HtmlElement, string>(currentElement, text)); } index--; continue; } if (index >= currentElement.Children.Count) { continue; } element = currentElement.Children[index]; switch (element.TagName) { case Matcher.TYPE_FRAME: case Matcher.TYPE_IFRAME: pushToStack(enumList, currentElement, ref index, ref domIndex); KeyValuePair <HtmlDocument, int>?lastFrame = frameIndex.Count == 0 ? (KeyValuePair <HtmlDocument, int>?)null : frameIndex.Peek(); HtmlWindow wnd = null; if (lastFrame != null) { int findedIndex = 0; foreach (var item in frameIndex) { if (item.Key.Window.Frames[item.Value].Document == element.Document) { while (findedIndex > 0) { frameIndex.Pop(); findedIndex--; } currentFrameIndex = 0; break; } else if (item.Key == element.Document) { currentFrameIndex = item.Value + 1; do { frameIndex.Pop(); findedIndex--; } while (findedIndex >= 0); break; } else { currentFrameIndex = 0; } findedIndex++; } } else { currentFrameIndex = 0; } frameIndex.Push(new KeyValuePair <HtmlDocument, int>(element.Document, currentFrameIndex)); wnd = element.Document.Window.Frames[currentFrameIndex]; currentElement = wnd.Document.Body; domCollection = ((IHTMLDOMNode)currentElement.DomElement).childNodes; break; case Matcher.TYPE_FORM: if (OnFormElementDetected(element) == false) { return; } break; case Matcher.TYPE_A: if (OnLinkElementFinded(element, textNode) == false) { return; } break; case Matcher.TYPE_SELECT: if (OnSelectElementFinded(element, textNode) == false) { return; } break; case Matcher.TYPE_INPUT: if (InputElementFinded(element, textNode) == false) { return; } break; case Matcher.TYPE_TEXTAREA: if (OnTextAreaElementDetected(element, textNode) == false) { return; } break; default: if (OnOtherElementFinded(element) == false) { return; } break; } if (node.childNodes.length > 0) { pushToStack(enumList, currentElement, ref index, ref domIndex); currentElement = element; domCollection = ((IHTMLDOMNode)currentElement.DomElement).childNodes; } } }