Search code examples
c#c++ui-automationmicrosoft-ui-automation

Getting the path of a button using UIAutomation


I'm trying to get a button of another app unique path because i want to save it in order to access it another time after i close the app on another run. I am interested in any possible way to save a path which i can follow to access the button automatically.

I'm getting the button like in the following code, but I don't know how to save its path (as in a log) in order to access it again.

GetCursorPos(&cursorPos);
CComPtr<IUIAutomationElement> elFromPoint;
_automation->ElementFromPoint(cursorPos, &elFromPoint);

Solution

  • There is no absolute "path" for controls in UI Automation. You can walk the automation tree starting from your element until you reach the desktop as shown here: How to walk automation tree, and create a path to the element as you traverse the tree. You can use this stored "path" to look for your element by either walking the tree from the desktop downwards or using the search mechanism provided by UI Automation to search for specific properties of your element. Bear in mind that some controls may not be identified for certain as there is no single property that functions as ID for the controls (AutomationId depends on the provider's implementation) and in that case you will need to implement a matching function between your stored element properties and the actual elements that you find.