public static UiElement FromGlobalId(string globalId) { //根据GlobalId返回UiElement元素 //解析XML,按顺序搜索,直到找到 globalId = globalId.Replace(Environment.NewLine, ""); XmlDocument xmlDoc = new XmlDocument(); XmlElement rootXmlElement = xmlDoc.CreateElement("Root"); xmlDoc.AppendChild(rootXmlElement); rootXmlElement.InnerXml = globalId; UiElement elementToFind = null; findUiElement(Desktop, rootXmlElement, ref elementToFind); return(elementToFind); }
// uiElement和子节点中去查找xmlElement对应的匹配的元素 private static bool findUiElement(UiElement uiElement, XmlElement _xmlElement, ref UiElement elementToFind) { var xmlElement = _xmlElement.CloneNode(true) as XmlElement; if (isUiElementMatch(uiElement, xmlElement.FirstChild as XmlElement)) { //xmlElement减去第一个xml节点 xmlElement.RemoveChild(xmlElement.FirstChild); if (!xmlElement.HasChildNodes) { //XML所有节点已经搜索完了 elementToFind = uiElement; return(true); } //xmlElement如果有idx属性,则优先跳到Idx对应的节点进行搜索 if ((xmlElement.FirstChild as XmlElement).HasAttribute("Idx")) { var idxStr = (xmlElement.FirstChild as XmlElement).Attributes["Idx"].Value; var idx = Convert.ToInt32(idxStr); var element = uiElement.GetUiElementByIdx(idx); if (element != null) { if (findUiElement(element, xmlElement, ref elementToFind)) { return(elementToFind != null); } } } foreach (var item in uiElement.Children) { if (findUiElement(item, xmlElement, ref elementToFind)) { break; } } } return(elementToFind != null); }
private void doSelect() { lock (this) { if (hasDoSelect) { return; } hasDoSelect = true; if (CurrentHighlightElement != null) { if (enableJavaUiNode(CurrentHighlightElement)) { return; } var element = new UiElement(CurrentHighlightElement); element.RelativeClickPos = CurrentHighlightElementRelativeClickPos; //隐藏窗体并截全屏,以便后期自己绘制红色标记矩形 this.Hide(); element.currentInformativeScreenshot = element.CaptureInformativeScreenshot(); //System.Diagnostics.Debug.WriteLine(string.Format("CurrentHighlightElementRelativeClickPos = ({0},{1})", CurrentHighlightElementRelativeClickPos.X, CurrentHighlightElementRelativeClickPos.Y)); System.Threading.Tasks.Task.Run(() => { this.Invoke(new Action(() => { UiElement.OnSelected?.Invoke(element); })); }); StopHighlight(); } } }
internal UiElement(UiNode node, UiElement parent = null) { this.uiNode = node; this.Parent = parent; this.boundingRectangle = node.BoundingRectangle; }
private static bool isUiElementMatch(UiElement uiElement, XmlElement xmlElement) { //System.Diagnostics.Debug.WriteLine(uiElement.Id+"$$$$$"+ xmlElement.OuterXml); //有可能ControlType也会改变,可用Node匹配任何的ControlType类型 if (xmlElement.Name == "Node" || xmlElement.Name == uiElement.ControlType) { bool isMatch = true; foreach (XmlAttribute attr in xmlElement.Attributes) { if (attr.Name == "Name") { if (attr.Value != uiElement.Name) { isMatch = false; break; } } else if (attr.Name == "AutomationId") { if (attr.Value != uiElement.AutomationId) { isMatch = false; break; } } else if (attr.Name == "UserDefineId") { if (attr.Value != uiElement.UserDefineId) { isMatch = false; break; } } else if (attr.Name == "ClassName") { if (attr.Value != uiElement.ClassName) { isMatch = false; break; } } else if (attr.Name == "Role") { if (attr.Value != uiElement.Role) { isMatch = false; break; } } else if (attr.Name == "Description") { if (attr.Value != uiElement.Description) { isMatch = false; break; } } else if (attr.Name == "ProcessName") { if (attr.Value != uiElement.ProcessName) { isMatch = false; break; } } else { } } return(isMatch); } return(false); }