Click here to Skip to main content
15,881,757 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi there!
I have a strange behaviour that I can't figure out how to solve.
The problem is that I would like to click 'File' menu button in Lightroom (in Notepad it works fine) but the Expand-pattern is throwing an exception although the 'File' button in fact do support the ExpandCollapse pattern (atleast Inspect.exe says so).

Below is short example of what Im trying to achieve (error handling and other stuff is left out for simplicity):

C#
private void ClickFileMenu()
    {
        try
        {
            // Application = Lightroom
            int processID = 4448;
           
            // Get root element (OK)
            var rootEelement = AutomationElement.RootElement.FindFirst(TreeScope.Descendants, new PropertyCondition(AutomationElement.ProcessIdProperty, processID));

            // Get 'File' menu element (OK)
            var fileMenuElement = rootEelement.FindFirst(TreeScope.Descendants, new PropertyCondition(AutomationElement.NameProperty, "File"));

            // Get the expand/collapse pattern (and yes, it exist according to Inspect.exe)
            ExpandCollapsePattern expandPattern = ExpandCollapsePattern)fileMenuElement.GetCurrentPattern(ExpandCollapsePattern.Pattern); // (OK)

            // Expand (FAIL)
            expandPattern.Expand();
        }
        catch (Exception exception)
        {
            // Exception occur with message: "Operation cannot be performed."
            var error = exception;
        }
    }

C#



Any ideas of what I'm doing wrong?

Regards Mr. Finch
Posted
Updated 2-Jan-19 16:59pm

1 solution

How to use:

 ElementWindows ew = new ElementWindows("My Window's Title");
          
ew.SelectMenu("My Main Menu Title", "My Menu Item");



In a specific Class:


private static void SelectMenu(AutomationElement rootElement, string Menu, string value)
       {
           AutomationElement lcMenu = rootElement.FindFirst(TreeScope.Descendants, new PropertyCondition(AutomationElement.NameProperty, Menu));

           ExpandCollapsePattern exPat = lcMenu.GetCurrentPattern(ExpandCollapsePattern.Pattern) as ExpandCollapsePattern;
           exPat.Expand();

           AutomationElement itemToSelect = lcMenu.FindFirst(TreeScope.Descendants, new PropertyCondition(AutomationElement.NameProperty, value));

           AutomationElement itemToSelect2 = itemToSelect.FindFirst(TreeScope.Subtree, new PropertyCondition(AutomationElement.NameProperty, value));

           InvokePattern invokePattern = itemToSelect2.GetCurrentPattern(InvokePatternIdentifiers.Pattern) as InvokePattern;

           invokePattern.Invoke();
       }

       public void SelectMenu(string Menu, string value)
       {

           ElementWindows.SelectMenu(m_Windows, Menu, value);
           Thread.Sleep(50);
       }


- by Ccting, Sibu, Sarawak, Malaysia
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900