UIAutomation will not retrieve child elements of an element

I see that an element with a specific Automation ID has children in the Inspect tool:

Inspect screenshot

But when I try to find them like this:

AutomationElement aPane = mainWindow.FindFirst(TreeScope.Subtree, new PropertyCondition(AutomationElement.AutomationIdProperty, "8264")); AutomationElementCollection theChildren = aPane.FindAll(TreeScope.Subtree, Condition.TrueCondition); 

The aPane element is aPane correctly, but theChildren element theChildren empty. Any ideas what went wrong?

+6
source share
5 answers

Actually the problem is that Inspect.exe is written in unmanaged , while I tried to achieve the same leads to managed code. Unmanaged code returns slightly different results than the managed version (for example, insidious code returned the document control type, where unmanaged code returned edit in my application).

It took me a while to figure this out, unmanaged code is much faster, more accurate, and therefore more reliable.

Some examples of unmanaged UI Automation code for C # can be found in the Microsoft Windows UI e Automation Unit. here

+1
source

In rare cases, I found that Find* calls do not find all automation objects. The only consecutive case I saw with this that controls WPF TextBlock is when it is not found in the data template by these calls. In these cases, you can try RawViewWalker , which is probably closer to what Inspect does inside.

 public static IEnumerable<AutomationElement> FindInRawView(this AutomationElement root) { TreeWalker rawViewWalker = TreeWalker.RawViewWalker; Queue<AutomationElement> queue = new Queue<AutomationElement>(); queue.Enqueue(root); while (queue.Count > 0) { var element = queue.Dequeue(); yield return element; var sibling = rawViewWalker.GetNextSibling(element); if (sibling != null) { queue.Enqueue(sibling); } var child = rawViewWalker.GetFirstChild(element); if (child != null) { queue.Enqueue(child); } } } 
+11
source

A bit late answer, but I wanted to fix the answer selected here. Yes, it is true that the VS provided by the COM shell may use a different UIAutomationClient.dll, and that the use of native code will be different from the managed code when calling the UIAutomation methods, but, nevertheless, the question asked here is another problem. (By the way, you can use the COM shell from managed code to call the correct version of the UIAutomation DLL, which will solve problems such as "inspect.exe finds, but my managed code cannot.").

I also ran into the problem posed here (mine: FindAll (TreeScope.Children, TrueCondition) did not return anything, although FindFirst () successfully returned children to the same control).

I tried using mike-z using RawViewWalker to find children, and it did a great job with this. I am writing this separate answer to say that it was not the Find * methods, but the difference between the FindAll and FindFirst methods that caused the August problem.

Update

Erratic behavior seems to be the norm when it comes to MS tools. The reason for this update is that I ran into a similar problem with my tlbimp.exe'd RCW for uia using C #, and this time I wrote direct equivalent C code, and, to my surprise, it worked fine while C # code refused to work anyway, trying to find simple OpenFileDialog controls, and then another control in the main form. The only difference between the two worlds is the mysterious magic of MC RCW. I'm not sure if this is the way marshaling is handled by automatically generated COM wrappers (tlbimp) or something else. But the attribute [ComConversionLoss], which appears for the created interface, does not suit me. In any case, I am now considering the possibility of manually creating a COM interface or converting the entire project into a native environment.

+2
source

The difference between managed and unmanaged UI automation is that the old implementation is with controlled use, but Inspect uses COM directly, and this is the newer version 3.0

+1
source

My original example is simplified. I tried to access the children using 3 methods:

  • RawViewWalker in .Net managed code.
  • Equivalent walker in COM, i.e. COM wrappers available in managed .Net code.
  • Non -.Net code (i.e. unmanaged code) in a completely separate VB6 application that I wrote.

Only the VB6 code (unmanaged) gave the same results as the Microsoft Inspect tool. I believe this confirms what others have said. There are serious issues with implementing Microsoft UI Automation in .NET. Perhaps the only solution to this is to write the UI Automation user client in .Net, but this assumes that the user interface automation servers in the target applications behave correctly. And this is not subject to control, because the target applications are written by other companies, not mine.

0
source

All Articles