16
AutomationElement child = walker.GetFirstChild(el);

using windows automation How do i simulator a left single click on Child ?

Stacker
  • 8,157
  • 18
  • 73
  • 135

3 Answers3

23

Rather than sending mouse events, you can Invoke it through the InvokePattern like so:

public void InvokeAutomationElement(AutomationElement automationElement)
{
    var invokePattern = automationElement.GetCurrentPattern(InvokePattern.Pattern) as InvokePattern;
    invokePattern.Invoke();
}
Levi
  • 839
  • 2
  • 7
  • 21
  • 4
    Not all windows support Invoke pattern – Max Dec 12 '12 at 12:56
  • 1
    This is true, but I'm guessing that if you can click on it you can do it with automation (and not mouse clicking). If it doesn't support the Invoke pattern, it probably supports the `SelectionItemPattern`, so you can `Select` it. – Levi Dec 15 '12 at 02:25
  • I've seen windows which only support WindowPattern and TransformPattern. From the question I cannot tell why the click is performed. It can be that Stacker wants to bring the window to focus (just a guess). So there's might be no immediate activity that is going to be "invoked". – Max Dec 15 '12 at 23:32
11

try with:

AutomationElement child = walker.GetFirstChild(el);
System.Windows.Point p = child.GetClickablePoint();
Mouse.Move((int)p.X, (int)p.Y);
Mouse.Click(MouseButton.Left);

Links:
AutomationElement.GetClickablePoint Method
Simulate mouse Enter/Move/Leave on WPF control without real mouse usage

Edit for comment

See this links:

Mouse.cs
NativeMethods.cs
Introduction to TestApi – Part 1: Input Injection APIs

shaune
  • 2,510
  • 1
  • 31
  • 36
andres descalzo
  • 14,887
  • 13
  • 64
  • 115
3

If the control has a "ClickablePoint" you can use this code

System.Windows.Point p = theButton.GetClickablePoint();
AutoItX3Lib.AutoItX3Class au3 = new AutoItX3Lib.AutoItX3Class();
au3.AutoItSetOption("MouseCoordMode", 0);
au3.MouseClick("LEFT", (int)p.X, (int)p.Y, 1, -1);

where AutoItX3Lib is the C# API for AutoIt (available on NuGet)

Fidel
  • 7,027
  • 11
  • 57
  • 81
Michael Bahig
  • 748
  • 8
  • 17