Ignoring UIAutomation ArgumentException

youki 996 Reputation points
2020-12-24T22:42:57.683+00:00

I'm getting an exception only in the output window in debug mode of the Element.FindAll:
Exception thrown: 'System.ArgumentException' in UIAutomationClientsideProviders.dll

The application keeps running and i'm getting the right element.
I can't see what's wrong here. Can/ should i ignore it?

        public static void GetElement()
        {
            AutomationElement element = AutomationElement.FromHandle(Window.GetHandle());
            AutomationElement parent = TreeWalker.ControlViewWalker.GetParent(element);

            try
            {
                if (parent != null)
                {
                    Condition conditions = new PropertyCondition(AutomationElement.AutomationIdProperty, "3");
                    AutomationElementCollection panes = parent.FindAll(TreeScope.Children, conditions);
                }
            }
            catch
            {
                throw;
            }
        }
Windows Forms
Windows Forms
A set of .NET Framework managed libraries for developing graphical user interfaces.
1,828 questions
C#
C#
An object-oriented and type-safe programming language that has its roots in the C family of languages and includes support for component-oriented programming.
10,245 questions
{count} votes

Accepted answer
  1. youki 996 Reputation points
    2020-12-31T00:28:48.237+00:00

    Ok,
    I have no exceptions anymore.
    As stated, i couldn't get the findFirst from the findFirst (hope it's understandable). I solved it by nextSibling in a while loop and the exception disapeared on the way. The time is down to below 20 milliseconds, may be i can optimize it.

    Thanks for the support, Kyle.

    0 comments No comments

2 additional answers

Sort by: Most helpful
  1. youki 996 Reputation points
    2020-12-25T23:37:45.43+00:00

    Hi Kyle,
    I meant that it's always freezing when i set a breakpoint and it stops at it. I'm usually doing it because i want to check the values in an object at the breakpoint. I think it's freezing because of the mousehook and the system has to process to much or something like that?!

    To the argument exception:
    I found out that it's excel related. I tested it in Chrome, IE, Word and only Excel throws the exception.
    It also throws the exception by a default condition like the following:

     Condition conditions = new AndCondition(new PropertyCondition(AutomationElement.IsEnabledProperty, true),
                                                                  new PropertyCondition(AutomationElement.ControlTypeProperty, ControlType.Pane));
    

    I don't know, if i have to give up the idea but the search is to slow.
    Even if i store the element after the element searching, i'm about +/- 80 milliseconds (the position is always changing, if it's an other workbook).

    I don't know, if there's another way to search an element faster, may be something like searching by Win Api and after finding, cast the object to an UIA element?!


  2. youki 996 Reputation points
    2020-12-30T15:06:55.007+00:00

    Hi Kyle,
    I can't use FindWindow because the name is changing so i'm using:

            public static IntPtr GetHandle()
            {
                IntPtr hWnd = IntPtr.Zero;
    
                if (NativeMethods.GetCursorPos(out Point point))
                {
                    hWnd = NativeMethods.WindowFromPoint(point);
                }
    
                return hWnd;
            }
    

    I've changed to IUIAutomation for speed and now i can't get the next sibling (with "TreeScope_Descendants") by the stated default condition after i found an element with the same condition before. Hm, didn't tested it but i think it's an Excel related bug again?!

    (I'm getting the "root element" by handle > findFirst (stated condition) > findFirst (stated condition fails, no element found))

    Element sequence changes by user so this would be the fastest way (in the last findFirst i could use findAll but that takes too long).
    Regards

    0 comments No comments