/// <summary> /// /// </summary> /// <param name="element"></param> /// <param name="Finder">优先finder设置</param> public void SetFindMethod(IHTMLElement element, FindAttribute Finder) { int count = 0; if (Finder != null) { FindMechanism.Add(Finder); count = GetElementCount(element.tagName); if (count > 1) { FindMechanism.Clear(); } else { SetFrameList(element); ElementType = TagStringToElementType(element); return; } } Finder = GetFinder(element, FindMethods.CssSelector); FindMechanism.Add(Finder); mshtml.IHTMLDOMNode node = element as mshtml.IHTMLDOMNode; count = GetElementCount(node.nodeName); //不支持CssSelector,试用xpath定位 if (count == -1) { FindMechanism.Clear(); Finder = GetFinder(element, FindMethods.XPath); FindMechanism.Add(Finder); } SetFrameList(element); ElementType = TagStringToElementType(element); }
private FindAttribute GetXPathFinder(IHTMLElement element) { FindAttribute Finder = Finder = new FindAttribute(); Finder.FindMethod = FindMethods.XPath; Finder.FindValue = XPathFinder.GetXPath(element); return(Finder); }
public void SetFindMethod(IHTMLElement element, FindMethods method, string value) { FindAttribute Finder = new FindAttribute(); Finder.FindMethod = method; Finder.FindValue = value; FindMechanism.Add(Finder); if (element == null) { return; } SetFrameList(element); ElementType = TagStringToElementType(element); }
private FindAttribute GetCssPathFinder(IHTMLElement element) { FindAttribute Finder = Finder = new FindAttribute(); Finder.FindMethod = FindMethods.CssSelector; string selector = element.getAttribute("__selector", 0) as string; if (!string.IsNullOrEmpty(selector)) { Finder.FindValue = selector; } else { Finder.FindValue = this.AppContext.Browser.Selector.GetCssPath(element, false); } return(Finder); }
protected FindAttribute GetFinder(IHTMLElement element, FindMethods method) { FindAttribute finder = new FindAttribute(); finder.FindMethod = method; if (method == FindMethods.XPath) { finder.FindValue = XPathFinder.GetXPath(element); } else if (method == FindMethods.CssSelector) { finder = GetCssPathFinder(element); } else { finder.FindValue = ActiveElementAttribute(element, method.ToString()); } return(finder); }
public override void LoadFromXml(XmlNode node) { base.LoadFromXml(node); string elementTypeString = node.SelectSingleNode("ElementType").InnerText; ElementType = (ElementTypes)Enum.Parse(typeof(ElementTypes), elementTypeString); XmlNode findernode = node.SelectSingleNode("FinderSet"); XmlNodeList finderList = null; if (findernode != null) { finderList = findernode.SelectNodes("Finder"); if (finderList != null) { foreach (XmlNode child in finderList) { var attribute = new FindAttribute(); attribute.FromXml(child); FindMechanism.Add(attribute); } } } XmlNode framenode = node.SelectSingleNode("Frames"); if (framenode != null) { finderList = framenode.SelectNodes("Finder"); if (finderList != null) { foreach (XmlNode child in finderList) { var attribute = new FindAttribute(); attribute.FromXml(child); FrameList.Add(attribute); } } } }
private void SetFrameList(IHTMLElement el) { List <IHTMLElement> frames = new List <IHTMLElement>(); IHTMLElement frame = BrowserExtensions.GetFrame(el); while (frame != null) { frames.Add(frame); frame = BrowserExtensions.GetFrame(frame); } foreach (var frameElement in frames) { FindAttribute attribute = GetFrameFinder((IHTMLElement)frameElement); if (attribute != null) { FrameList.Add(attribute); } } FrameList.Reverse(); }
/// <summary> /// frame不支持css selector查找 /// </summary> /// <param name="element"></param> /// <returns></returns> FindAttribute GetFrameFinder(IHTMLElement element) { if (!string.IsNullOrEmpty(ActiveElementAttribute(element, "id"))) { FindAttribute Finder = Finder = new FindAttribute(); Finder.FindMethod = FindMethods.Id; Finder.FindValue = ActiveElementAttribute(element, "id"); return(Finder); } else if (!string.IsNullOrEmpty(ActiveElementAttribute(element, "name"))) { FindAttribute Finder = Finder = new FindAttribute(); Finder.FindMethod = FindMethods.Name; Finder.FindValue = ActiveElementAttribute(element, "name"); return(Finder); } else { return(GetXPathFinder(element)); } }
//取元素属性集合 public FindAttribute GetFinder(IHTMLElement element) { FindAttribute Finder = null; foreach (var method in AppSettings.Instance.FindPattern) { if (IsMethodUsed(method)) { continue; } Finder = new FindAttribute(); Finder.FindValue = ActiveElementAttribute(element, method.ToString()); Finder.FindMethod = method; if (Finder.FindValue != "") { break; } else { Finder = null; } } return(Finder); }
static public Constraint GetConstraint(FindAttribute attribute) { string findvalue = attribute.FindValue; Constraint constraint = null; switch (attribute.FindMethod) { case FindMethods.Alt: constraint = Find.ByAlt(findvalue); break; case FindMethods.Class: constraint = Find.ByClass(findvalue); break; case FindMethods.For: constraint = Find.ByFor(findvalue); break; case FindMethods.Id: constraint = attribute.Regex ? Find.ById(new System.Text.RegularExpressions.Regex(findvalue)) : Find.ById(findvalue); break; case FindMethods.Index: try { constraint = Find.ByIndex(Convert.ToInt32(findvalue)); } catch { MessageBox.Show(string.Format("值{0}格式不正确", findvalue)); } break; case FindMethods.Name: constraint = attribute.Regex ? Find.ByName(new System.Text.RegularExpressions.Regex(findvalue)):Find.ByName(findvalue); break; case FindMethods.Src: constraint = Find.BySrc(findvalue); break; case FindMethods.Text: constraint = Find.ByText(findvalue); break; case FindMethods.Title: constraint = Find.ByTitle(findvalue); break; case FindMethods.Url: constraint = Find.ByUrl(findvalue); break; case FindMethods.Value: constraint = Find.ByValue(findvalue); break; case FindMethods.Href: constraint = attribute.Regex ? Find.By("href", new System.Text.RegularExpressions.Regex(findvalue)) : Find.By("href", findvalue); break; case FindMethods.Custom: constraint = Find.By(attribute.FindName, findvalue); break; case FindMethods.XPath: constraint = new XPathConstraint(findvalue); break; case FindMethods.ProximityText: constraint = new ProximityTextConstraint(findvalue); break; case FindMethods.CssSelector: constraint = Find.BySelector(findvalue); break; } return(constraint); }