Search code examples
c#ui-automation

Automate click on button


I am trying to click a button in an application from a .Net 7 c# program in windows. The button has only the property IsLegacyIAccessiblePatternAvailable = true, which I can see in the windows Inspect.exe. Thus, I did try to invoke the button with:

if (button_Create.TryGetCurrentPattern(LegacyIAccessiblePattern.Pattern, out var pattern))
{
  var legacyPattern = (System.Windows.Automation.LegacyIAccessiblePattern)pattern;
  legacyPattern.DoDefaultAction();
}

However, I cannot get this to work because LegacyIAccessiblePattern is not found with error CS0103 and CS0234. Other methods from System.Windows.Automation however are available.

What do I have to do to tell VS to include the correct library?


Solution

  • Are you using the v3.0 of the Windows Automation API? I found the projects UIAComWrapper and UIAutomation-Interop that enable the use of classes in olders versions of the API, and they include LegacyIAccessiblePattern. So it might be the case that you trying to use a class that is only available in old versions of the API.

    You might want to try to reference the NuGet package for the second one, Interop.UIAutomationClient and see if it does the trick.