private void button1_Click(object sender, EventArgs e) { //label1.Text = DateTime.Now.ToLongTimeString(); var criteria = "//ComboBox[@AutomationId='comboBox1']/List/ListItem[contains(normalize-space(.), " + EscapeQuotes("6 7") + ")]"; comboBox1.Focus(); comboBox1.DroppedDown = true; //AutoIt.AutoItX.MouseMove(Location.X + comboBox1.Location.X + comboBox1.Width - 2, Location.Y + comboBox1.Location.Y + 38, 2); //AutoIt.AutoItX.MouseMove(Location.X + comboBox1.Location.X + comboBox1.Width - 2, Location.Y + comboBox1.Location.Y + 39, 2); //AutoIt.AutoItX.MouseClick(x: Location.X + comboBox1.Location.X + comboBox1.Width - 2, y: Location.Y + comboBox1.Location.Y + 38);//.ControlClick(Handle, comboBox1.Handle, x: comboBox1.Width - 2, y: 2); try { var walker = new AutomationElementTreeWalker(new XPathParser <IXPathExpression>().Parse(criteria, new WalkerBuilder())); var rootElement = AutomationElement.FromHandle(Handle); var src = new CancellationTokenSource(); var found = walker.Find(rootElement, src.Token).ToList(); MessageBox.Show("Found " + found.Count); } catch (Exception ex) { MessageBox.Show(ex.Message); } }
public IEnumerable <AutomationElement> Find(AutomationElement automationElement, CancellationToken cancellationToken) { var walker = new AutomationElementTreeWalker(new XPathParser <IXPathExpression>().Parse(_criteria, new WalkerBuilder())); return(new BreadthFirstSearch() .Find(_rootElement, ControlType.Window, cancellationToken) .Where(w => w.Current.ControlType == ControlType.Window) .SelectMany(window => walker.Find(window, cancellationToken))); }
public IEnumerable <AutomationElement> Find(AutomationElement automationElement, string mechanism, string criteria, CancellationToken cancellationToken) { System.Diagnostics.Debug.WriteLine($"FindElements {mechanism} '{criteria}'... ({automationElement.ToDiagString()})"); switch (mechanism) { case "id": mechanism = "xpath"; break; case "css selector": if (_elementIdSelectorRegex.IsMatch(criteria)) { mechanism = "xpath"; criteria = $"//*[@AutomationId='{criteria}']"; break; } if (_tagNameSelectorRegex.IsMatch(criteria)) { var controlType = criteria.AsControlType(); if (controlType == ControlType.Window && automationElement.Current.ControlType == controlType) { return(new List <AutomationElement> { automationElement }); } mechanism = "xpath"; criteria = $"//{criteria}"; break; } throw new InvalidSelectorException(mechanism + " " + criteria + ", only tag name and id selectors are allowed"); case "xpath": break; case "tag name": mechanism = "xpath"; criteria = $"//{criteria}"; break; default: throw new InvalidSelectorException(mechanism + " " + criteria); } try { System.Diagnostics.Debug.WriteLine($"FindElements {mechanism} '{criteria}'..."); var walker = new AutomationElementTreeWalker(new XPathParser <IXPathExpression>().Parse(criteria, new WalkerBuilder())); return(walker.Find(automationElement, cancellationToken)); } finally { } }