Microsoft UI自动化无法获取chrome的上下文菜单元素 [英] Microsoft ui-automation not able to fetch chrome's context menu elements

查看:365
本文介绍了Microsoft UI自动化无法获取chrome的上下文菜单元素的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

Why UIAutomation is not able to fetch chrome's context menu elements.

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

C# Code: The below code will subscribe to the root element.

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

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

在使用谷歌浏览器的情况下,波纹管事件不会触发,但在其他情况下(例如IE或Firefox或任何其他应用程序)就可以了.

The bellow event is not getting fired in the case of google chrome, but in other cases (i.e. IE or Firefox or any other application) it is fine.

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

是否需要更改代码或有其他替代解决方案来解决此问题?

Is there any code changes required or is there any alternative solution for this problem?

推荐答案

我已经通过使用称为FromPoint()的方法实现了此任务.我的用例是获取右键单击并粘贴事件.

I have achieved this task by using a method called as FromPoint(). My use case was to get right click and paste event.

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

Step 1: Subscribe to the menu open and close event:

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计时器.

Step 2: When MenuOpenedEvent start timer which will get the current location of your mouse and on MenuCloseEvent strop timer.

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

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

Step 3: Get the element at the mouse location

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

这篇关于Microsoft UI自动化无法获取chrome的上下文菜单元素的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆