Microsoft UIAutomation isn't ALWAYS working on some computers. C#

纵然是瞬间 提交于 2019-12-10 12:18:10

问题


I have some C# code for extracting url from Chrome. It usually works on Win7/Win 8.1, but on some computers with the same configuration it doesn't work. And, probably, there is no difference between these configurations. Why does it happen?

Process[] procsChrome = Process.GetProcessesByName("chrome");
foreach(Process chrome in procsChrome)
{
   if(chrome.MainWindowHandle == IntPtr.Zero)
   {
    continue;
   }
   AutomationElement mainWindow = AutomationElement.FromHandle(chrome.MainWindowHandle);
   elmUrlBar = mainWindow.FindFirst(TreeScope.Descendants,  new PropertyCondition(AutomationElement.ControlTypeProperty, ControlType.Edit));

  //elmUrlBar on some computers inited, on some NULL. But versions of Chrome are identical.
...
}

回答1:


It's possible that this is a Windows security issue. There is some security in Windows to disable communication from one application to another, called User Account Control (UAC).

If you usually start Visual Studio as an Administrator, then the child process it spawns to run your application will also be elevated, and your automation will likely work.

Trying it on another machine will likely fail because of UAC. Or it might succeed on a machine that has some UAC settings disabled.

On the machines that fail, I would try to temporarily disable UAC as mentioned here, and if it now works, then you know that's the issue. You can actually get it working even when UAC is enabled, by adding a manifest to your application as mentioned here.




回答2:


(1) Have you verified that Accessibility was turned on in Chrome in those situations where your application failed? It either must be enabled from the Chrome's command line or turned on for individual tabs.

(2) You may have run afoul of User Interface Privilege Isolation (UIPI). It's not a problem if you're running an Admin login, but with a Standard login the requirements to bypass UIPI are:

  1. Have uiAccess=True in the application's manifest.
  2. Sign the application via a valid code signing authority. (You can test with a self-signed executable however--we have done that successfully.)
  3. Install the executable file to the System32 directory (or SysWOW64 for 32-bit applications installed on 64-bit windows).


来源:https://stackoverflow.com/questions/25613115/microsoft-uiautomation-isnt-always-working-on-some-computers-c-sharp

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!