简体   繁体   中英

Using “Inspect.exe” can't find some elements, why?

I want to use c # write a UI Automation program, but I can't find some elements with "Inspect.exe", Can't find pictures of text labels(eg. image1), why?

image1: https://i.stack.imgur.com/NqpKA.png

image2: https://i.stack.imgur.com/2PIuj.png

Code sample:

var desktop = AutomationElement.RootElement;

var condition = new PropertyCondition(AutomationElement.NameProperty, "Customer Register");

var window = desktop.FindFirst(System.Windows.Automation.TreeScope.Children, condition);

The code you posted for getting the elements is correct from what I can see in your screen shot from inspect. I have a feeling that the UI Automation is not getting the name back because of the text encoding. Unless you have access to the source and can mess with how it gets and sets text for those labels it won't be possible to retrieve the text through UI Automation.

You could use the code you have and use the native window handle for the window with the win32 api to get the text.

 [DllImport("user32.dll", CharSet = CharSet.Auto)]
 static extern IntPtr SendMessage(IntPtr hWnd, UInt32 Msg, IntPtr wParam, [Out] StringBuilder lParam);
 public static string GetWindowTextRaw(IntPtr hwnd)
 {
     // Allocate correct string length first
     int length = (int)SendMessage(hwnd, WM_GETTEXTLENGTH, IntPtr.Zero, IntPtr.Zero);
     StringBuilder sb = new StringBuilder(length + 1);
     SendMessage(hwnd, WM_GETTEXT, (IntPtr)sb.Capacity, sb);
     return sb.ToString();
 }

 public static void YourMethod() 
 {
     var desktop = AutomationElement.RootElement;

     var process = Process.Start("Path/To/Your/Process.exe");

     var condition = new PropertyCondition(AutomationElement.ProcessId, process.Id);

     var window = desktop.FindFirst(System.Windows.Automation.TreeScope.Children, condition);

     var windowTitle = GetWindowTextRaw(window.NativeWindowHandle)
 }

Sources:

GetWindowText
SendMessage

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