Microsoft ui-automation无法获取chrome的上下文菜单元素

时间:2017-01-04 11:58:22

标签: c# .net google-chrome microsoft-ui-automation

为什么UIAutomation无法获取chrome的上下文菜单元素。

C#代码: 以下代码将订阅根元素。

 public void SubscribeToInvoke()
        {
            Automation.AddAutomationEventHandler(AutomationElement.MenuOpenedEvent,
                    AutomationElement.RootElement,
                    TreeScope.Descendants, UIAEventHandler);

            Automation.AddAutomationEventHandler(AutomationElement.MenuClosedEvent,
                    AutomationElement.RootElement,
                    TreeScope.Descendants, UIAEventHandler);
        }

在谷歌浏览器的情况下,波纹管事件并没有被解雇,但在其他情况下(例如IE或Firefox或任何其他应用程序)它没问题。

        private void UIAEventHandler(object sender, AutomationEventArgs e)
        {
            AutomationElement sourceElement;
            sourceElement = sender as AutomationElement;
            if (e.EventId == AutomationElement.MenuOpenedEvent)
            {
            }
            else if (e.EventId == AutomationElement.MenuClosedEvent)
            {
            }
        }

是否需要更改代码或是否有针对此问题的替代解决方案?

1 个答案:

答案 0 :(得分:2)

我通过使用名为FromPoint()的方法完成了此任务。我的用例是右键单击并粘贴事件。

第1步:订阅菜单打开和关闭事件:

public void SubscribeToInvoke()
        {
            Automation.AddAutomationEventHandler(AutomationElement.MenuOpenedEvent,
                    AutomationElement.RootElement,
                    TreeScope.Descendants, UIAEventHandler);

Automation.AddAutomationEventHandler(AutomationElement.MenuClosedEvent,
                    AutomationElement.RootElement,
                    TreeScope.Descendants, UIAEventHandler);
        }

步骤2:当MenuOpenedEvent启动计时器时,它将获取鼠标的当前位置和MenuCloseEvent strop计时器。

if (e.EventId == AutomationElement.MenuOpenedEvent)
            {
                timer_Chrome.Enabled = true;
            }
            else if (e.EventId == AutomationElement.MenuClosedEvent)
            {
                timer_Chrome.Enabled = false;
             }

步骤3:在鼠标位置获取元素

            System.Windows.Point point = new System.Windows.Point(Cursor.Position.X, Cursor.Position.Y);
            AutomationElement sourceElement = AutomationElement.FromPoint(point);