none
Microsoft ui-automation not able to fetch chrome's context menu elements RRS feed

  • Question

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

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

    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?


    Wednesday, January 4, 2017 12:02 PM

Answers

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

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

    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;
                 }

    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);
    Tuesday, January 10, 2017 4:51 AM

All replies

  • Chrome's UI may be HTML depending on what you are doing.  For example, in the bookmark manager, right clicks are not opening up Windows native popup menus, they are just rendering HTML.

    So which context menu's elements are you hoping to fetch?

    Wednesday, January 4, 2017 1:46 PM
  • It can be any context menu (when we right click) within the chrome browser.

     
    Wednesday, January 4, 2017 2:02 PM
  • It can be any context menu (when we right click) within the chrome browser.

     

    Sorry.  That can't work then.

    I cite the bookmark manager as a counterexample where it uses HTML UI.

    Wednesday, January 4, 2017 2:05 PM
  • So u r telling, all context menus (bookmark manager, settings menu or when we right click) within the chrome browser is custom context menu (i.e. made of HTML), that's why UI-Automation is not able to fetch the elements? Is there any other way to achieve this task, like by creating any extension in the chrome browser?


    Wednesday, January 4, 2017 2:13 PM
  • So u r telling, all context menus (bookmark manager, settings menu or when we right click) within the chrome browser is custom context menu (i.e. made of HTML), that's why UI-Automation is not able to fetch the elements? Is there any other way to achieve this task, like by creating any extension in the chrome browser?


    That's right.  And no there's no robust way to achieve this -- certainly not one that's resistant to any update to what Chrome might become tomorrow.

    And that's certainly not a .NET Framework Class Libraries question.  It's beyond the scope of this forum, in my opinion.

    Wednesday, January 4, 2017 6:46 PM
  • I have achieved this task by using a method called as FromPoint(). My use case was to get right click and paste event.

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

    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;
                 }

    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);
    Tuesday, January 10, 2017 4:51 AM