简体   繁体   中英

UIAutomation - InvokePattern only works while Debugging - Button is not Recognized as a Button

I am manipulating an old program using UIAutomation . The issue I have involves pressing a button using InvokePattern .

After pressing a button that opens up an explorer window and getting the AutomationElement for the window, I get the AutomationELement for an "Open" button, actually a SplitButton . I can find the element easily, but it comes up as a Pane control rather than a SplitButton Control. However, if I insert a breakpoint before looking for the Button element and manually step through the code in Debug mode, the "Open" button is recognized as a Button.

If I insert a breakpoint after finding the Button element, the element Name and AutomationID are correct, but ControlType is a Pane instead of a Button. It doesn't matter if I put in a delay after getting the explorer window, it only works while debugging. It's bizarre.

The offending code is below:

InvokePattern bPattern = (InvokePattern)button.GetCurrentPattern(InvokePattern.Pattern);
        bPattern.Invoke();

        for (int wait = 0; wait < 50; wait++)
        {
            if (explorerWindow != null)
                break;

            explorerWindow = reportWindow.FindFirst(TreeScope.Children,
                                            new PropertyCondition(AutomationElement.NameProperty, "Select Report"));

            Thread.Sleep(200);
        }

        explorerOpenButton = explorerWindow.FindFirst(TreeScope.Children,
                                new PropertyCondition(AutomationElement.NameProperty, "Open"));

Try creating an AndCondition including two PropertyCondition s, one for the button's name and the other for the button's class name ( SplitButton ?). Put that logic inside a while/for loop like you did when looking for the window. Like so:

var andCondition = new AndCondition(new PropertyCondition(AutomationElement.NameProperty, "Open"), new PropertyCondition(AutomationElement.ClassNameProperty, "SplitButton"));

I think it's possible that there are two elements with the same Name but with different class names appearing somehow in the same hierarchy in the visual tree.

If that doesn't help, try querying the RuntimeId property in the same manner and see if it ever returns.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM