{"id":948,"date":"2020-07-03T21:47:00","date_gmt":"2020-07-03T13:47:00","guid":{"rendered":"http:\/\/blog.5danyuan.com\/?p=948"},"modified":"2020-04-03T22:23:35","modified_gmt":"2020-04-03T14:23:35","slug":"%e8%8e%b7%e5%8f%96chrome%e5%bd%93%e5%89%8d%e6%a0%87%e7%ad%be%e9%a1%b5%e7%9a%84%e5%9c%b0%e5%9d%80","status":"publish","type":"post","link":"https:\/\/blog.5danyuan.com\/?p=948","title":{"rendered":"\u83b7\u53d6chrome\u5f53\u524d\u6807\u7b7e\u9875\u7684\u5730\u5740"},"content":{"rendered":"\n<p>\u5728\u8bc4\u4f30\u4e00\u4e2ademo\u7684\u65f6\u5019\uff0c\u6709\u4e00\u4e2a\u9700\u6c42\u662f\u8981\u83b7\u5f97\u7528\u6237\u5f53\u524d\u6d4f\u89c8\u5668\u6b63\u5728\u8bbf\u95ee\u7684\u7f51\u9875\u662f\u5426\u547d\u4e2d\u6211\u4eec\u7684\u767d\u540d\u5355\uff0c\u5982\u679c\u662f\u7684\u8bdd\uff0c\u5219\u4f1a\u5f00\u542f\u4e00\u7cfb\u5217\u7684\u4f18\u5316\u6a21\u5f0f\u8bbe\u5b9a\u3002<\/p>\n\n\n\n<p>\u7ecf\u8fc7\u4e00\u756a\u641c\u7d22\uff0c\u53d1\u73b0\u53ef\u4ee5\u4f7f\u7528\u5fae\u8f6f\u7684\u81ea\u52a8\u5316\u6d4b\u8bd5\u6846\u67b6\u6765\u95f4\u63a5\u6ee1\u8db3\u6211\u4eec\u7684\u8981\u6c42\u3002<\/p>\n\n\n\n<p>MS UI Automation\u662fMSAA\u6280\u672f\u7684\u4e00\u4e2a\u66ff\u4ee3\u54c1\uff1a\u5373\u8ba9\u63a7\u4ef6\u548c\u5e94\u7528\u7a0b\u5e8f\u5177\u6709\u66f4\u597d\u7684\u53ef\u8fbe\u6027\uff08accessible\uff09\u3002\u7b80\u5355\u6765\u8bb2\uff0c\u5b83\u5c31\u662f\u51e0\u4e2adll\uff0c\u63d0\u4f9b\u4e86\u4e00\u5957API\u548cInterface\uff0c\u53ca\u5176\u76f8\u5e94\u7684\u6a21\u5f0f\uff0c\u8ba9\u8f6f\u4ef6\u7684\u5f00\u53d1\u8005\u9075\u5faa\u8be5\u6a21\u5f0f\u53bb\u5b9e\u73b0\u76f8\u5e94\u7684interface\uff0c\u4ece\u800c\u8f6f\u4ef6\u7684\u4f7f\u7528\u8005\uff08\u4e0d\u4ec5\u4ec5\u662f\u5ba2\u6237\uff0c\u8fd8\u5305\u62ec\u4f8b\u5982\u6d4b\u8bd5\u4eba\u5458\u60f3\u7f16\u5199\u4e00\u4e9b\u81ea\u52a8\u5316\u6d4b\u8bd5\u4ee3\u7801\u6765\u5b8c\u6210\u7a0b\u5e8f\u76f8\u5173\u7684\u4e1a\u52a1\u903b\u8f91\uff09\u80fd\u66f4\u597d\u7684\u4f7f\u7528\u8be5\u8f6f\u4ef6\u3002<\/p>\n\n\n\n<p>\u5728UI Automation\u4e2d\uff0c\u6240\u6709\u7684\u7a97\u4f53\u3001\u63a7\u4ef6\u90fd\u8868\u73b0\u4e3a\u4e00\u4e2aAutomationElement\uff0c AutomationElement \u4e2d\u5305\u542b\u6b64\u63a7\u4ef6\u6216\u7a97\u4f53\u7684\u5c5e\u6027\uff0c\u5728\u5b9e\u73b0\u81ea\u52a8\u5316\u7684\u8fc7\u7a0b\u4e2d\uff0c\u6211\u4eec\u901a\u8fc7\u5176\u76f8\u5173\u5c5e\u6027\u8fdb\u884c\u5bf9\u63a7\u4ef6\u81ea\u52a8\u5316\u64cd\u4f5c\u3002\u5bf9\u4e8eUI\u7528\u6237\u754c\u9762\u6765\u8bf4\uff0c\u6240\u6709\u663e\u793a\u5728\u684c\u9762\u4e0a\u7684UI\uff0c\u5176\u5b9e\u9645\u662f\u4e00\u4e2aUI Tree\uff0c\u6839\u8282\u70b9\u662fdesktop\u3002\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528UI Spy\u6216\u8005\u662fSPY++\u6765\u83b7\u5f97Window\u548cControl\u7684\u76f8\u5173\u4fe1\u606f\u3002\u5728UI Automation\u91cc\uff0c\u6839\u8282\u70b9\u8868\u793a\u4e3aAutomationElemnet.RootElement. \u901a\u8fc7\u6839\u8282\u70b9\uff0c\u6211\u4eec\u53ef\u4ee5\u901a\u8fc7\u7a97\u4f53\u6216\u63a7\u4ef6\u7684Process Id\u3001Process Name\u6216\u8005Window Name\u627e\u5230\u76f8\u5e94\u7684\u5b50AutomationElement\uff0c\u4f8b\u5982 Dialog\u3001Button\u3001 TextBox\u3001Checkbox\u7b49\u6807\u51c6\u63a7\u4ef6\uff0c\u901a\u8fc7\u63a7\u4ef6\u6240\u5bf9\u5e94\u7684Pattern\u8fdb\u884c\u76f8\u5173\u7684\u64cd\u4f5c\u3002<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>Process&#91;] procsChrome = Process.GetProcessesByName(\"chrome\");\n\nif (procsChrome.Length &lt;= 0)\n{\n    Console.WriteLine(\"\u672a\u627e\u5230 chrome \u8fdb\u7a0b\");\n}\n\nforeach (Process proc in procsChrome)\n{\n    \/\/ the chrome process must have a window \n    if (proc.MainWindowHandle == IntPtr.Zero)\n    {\n        continue;\n    }\n\n    AutomationElement root = AutomationElement.FromHandle(proc.MainWindowHandle);\n    Condition conditions = new AndCondition(\n        new PropertyCondition(AutomationElement.ProcessIdProperty, proc.Id),\n        new PropertyCondition(AutomationElement.IsControlElementProperty, true),\n        new PropertyCondition(AutomationElement.IsContentElementProperty, true),\n        new PropertyCondition(AutomationElement.ControlTypeProperty, ControlType.Edit)\n    );\n\n    AutomationElement bar = root.FindFirst(TreeScope.Descendants, conditions);\n\n    if(bar != null)\n    {\n        var url = bar.GetCurrentPropertyValue(ValuePatternIdentifiers.ValueProperty).ToString();\n        Console.WriteLine(url);\n    }\n}<\/code><\/pre>\n\n\n\n<p>\u4e0d\u8fc7\uff0c\u6839\u636e\u7f51\u4e0a\u7684\u8bf4\u6cd5\uff0c\u8fd9\u6837\u7684\u4ee3\u7801\u53ea\u5728v53\u4ee5\u4e0a\u7248\u672c\u624d\u6709\u6548\u679c\u3002\u76ee\u524d\u6d4f\u89c8\u5668\u7248\u672c\u90fd7\u300180\u4e86\uff0c\u5bf9\u4e8e\u4e00\u4e2ademo\u800c\u8a00\uff0c\u5e94\u8be5\u53ef\u4ee5\u6ee1\u8db3\u5927\u90e8\u5206\u8981\u6c42\u4e86\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u5728\u8bc4\u4f30\u4e00\u4e2ademo\u7684\u65f6\u5019\uff0c\u6709\u4e00\u4e2a\u9700\u6c42\u662f\u8981\u83b7\u5f97\u7528\u6237\u5f53\u524d\u6d4f\u89c8\u5668\u6b63\u5728\u8bbf\u95ee\u7684\u7f51\u9875\u662f\u5426\u547d\u4e2d\u6211\u4eec\u7684\u767d\u540d\u5355\uff0c\u5982\u679c\u662f\u7684\u8bdd\uff0c\u5219\u4f1a [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[5],"tags":[319],"class_list":["post-948","post","type-post","status-publish","format-standard","hentry","category-daily","tag-automationelement"],"_links":{"self":[{"href":"https:\/\/blog.5danyuan.com\/index.php?rest_route=\/wp\/v2\/posts\/948","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/blog.5danyuan.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/blog.5danyuan.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/blog.5danyuan.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/blog.5danyuan.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=948"}],"version-history":[{"count":0,"href":"https:\/\/blog.5danyuan.com\/index.php?rest_route=\/wp\/v2\/posts\/948\/revisions"}],"wp:attachment":[{"href":"https:\/\/blog.5danyuan.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=948"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blog.5danyuan.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=948"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blog.5danyuan.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=948"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}