MS UI Automation offers InvokePattern for controls that are considered 'click-ready'. If a control supports InvokePattern, we are aware whether the control accepted click on didn't. Use of this pattern is extremely easy:
Get-UIAWindow -pn calc | Get-UIAButton -Name 1 | Invoke-UIAButtonClick;
There is an option for output. By default, the Invoke-UIA[ControlType]Click cmdlet returns the control(s) a successful click was performed against to the pipeline:
Get-UIAWindow -pn calc | Get-UIAButton -Name 1 | Invoke-UIAButtonClick | Read-UIAControlName;
Similarly, we can click on every numerical button, one by one:
Get-UIAWindow -pn calc | Get-UIAButton -Name [1-9] | Invoke-UIAButtonClick | Read-UIAControlName;
This behavior can be changed to output the true/false result ($true on success and $false on fail):
if (Get-UIAWindow -pn calc | Get-UIAButton -Name 1 | Invoke-UIAButtonClick -PassThru:$false) {
    # on successful click
Alternatively, an if condition could be built around null/not null state:
if ($null -ne (Get-UIAWindow -pn calc | Get-UIAButton -Name 1 | Invoke-UIAButtonClick)) {
    # on successful click
As it's said on the InvokePattern page, if a control supports another pattern with similar behavior (for example ExpandCollapsePattern for tree items or menu items), there is no InvokePattern provided.

Tags: ControlType.Window, ControlType.Button, InvokePattern

Last edited Jan 19, 2013 at 5:22 PM by xinliu, version 8


No comments yet.