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

允我心安 提交于 2019-12-11 23:10:11

问题


  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:


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());


来源:https://stackoverflow.com/questions/20682035/unable-to-click-a-button-even-though-i-am-able-to-retrieve-it-using-the-name

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!