I want to get the text "Running" which is under the property Value.Value. However, this is not part of the properties listed in the available fields of the AutomationElement Class.
How to get a custom property value which cannot be accessed using AutomationElement properties in c#
Asked
Active
Viewed 458 times
1

Leonardo Alves Machado
- 2,747
- 10
- 38
- 53
1 Answers
0
The managed UI Automation API is a bit strange and tricky to use (aside: TBH, it’s quite terrible as an API and needs to be overhauled, using it for any amount of time and you’ll find yourself wrapping it with your own methods that make it easier to use, maybe someone has done this a put it in a nice library, IDK).
To retrieve property values (or call available methods) you first need to get the ‘pattern’ for the AutomationElement
. The pattern we are interested in is the ValuePattern
element.GetCurrentPattern(ValuePattern.Pattern);
This method annoyingly returns a plain object
that we need to cast, so we can get the Value. Using C#7 syntax this is slightly less painful:
if(element.GetCurrentPattern(ValuePattern.Pattern) is ValuePattern valuePattern)
{
Console.WriteLine(valuePattern.Current.Value);
}

Dave M
- 2,863
- 1
- 22
- 17