Unable to click a button even though I am able to retrieve it using the Name

431 views Asked by At
  var navigator = BonusAppControllers.EbsControl.CurrentApp.GetWindow("XWindow").Get(SearchCriteria.ByAutomationId("AnimatedExplorerNavigator"));
  AutomationElement dashboardElement = navigator.AutomationElement.FindFirst(TreeScope.Subtree,SearchConditionFactory.CreateForName("NavigateLink_1").AutomationCondition);
  var dashBoardBtn = new Button(dashboardElement, navigator.ActionListener);
  dashBoardBtn.Click();

I have this code for the button with Name as NavigateLink_1. Now when i run this and debug it, I find that I am able to get the correct button instance in the dashBoardBtn variable but the Click() function isn't working. Even if i try to do all this just by using the button name as in using Get(SearchCriteria.ByText("NavigateLink_1")) I face the same problem. The same thing I tried with some other button part of the same group of buttons but it's working fine in that case.

Can Anyone suggest me what could be the problem.I am using the White Framework and UI Spy as the UI inspector for my application

1

There are 1 answers

0
Chaser324 On

I haven't used White, but I have used the native UIA libraries. The issue you'll occasionally run into with it is that you'll have an object that is clickable, but depending on how the click is being handled internally, you may not necessarily be able to use the InvokePattern to perform a click. That might be the case here.

As an alternative, you can use some code to move the mouse cursor over the AutomationElement and issue a click using P/Invoke. It's a bit of a hack, but it's often the simplest option when you run into this problem.

private const UInt32 MOUSEEVENTF_LEFTDOWN = 0x0002;
private const UInt32 MOUSEEVENTF_LEFTUP = 0x0004;

[DllImport("user32.dll")]
private static extern void mouse_event(UInt32 dwFlags, UInt32 dx, UInt32 dy, UInt32 dwData, IntPtr dwExtraInfo);

...
...

AutomationElement buttonToClick;

...
...

Cursor.Position = buttonToClick.GetClickablePoint();
mouse_event(MOUSEEVENTF_LEFTDOWN, 0, 0, 0, new IntPtr());
mouse_event(MOUSEEVENTF_LEFTUP, 0, 0, 0, new IntPtr());