{"id":6170,"date":"2015-09-01T14:36:07","date_gmt":"2015-09-01T05:36:07","guid":{"rendered":"http:\/\/www.ka-net.org\/blog\/?p=6170"},"modified":"2015-09-01T14:36:07","modified_gmt":"2015-09-01T05:36:07","slug":"webdriver%e3%82%92%e4%bd%bf%e3%82%8f%e3%81%9amicrosoft-edge%e3%82%92%e5%88%b6%e5%be%a1%e3%81%99%e3%82%8bc%e3%82%b3%e3%83%bc%e3%83%89","status":"publish","type":"post","link":"https:\/\/www.ka-net.org\/blog\/?p=6170","title":{"rendered":"WebDriver\u3092\u4f7f\u308f\u305aMicrosoft Edge\u3092\u5236\u5fa1\u3059\u308bC#\u30b3\u30fc\u30c9"},"content":{"rendered":"<p><a href=\"\/\/www.ka-net.org\/blog\/?p=6033\" title=\"Microsoft Edge\u3092\u64cd\u4f5c\u3059\u308bVBA\u30de\u30af\u30ed(DOM\u7de8) | \u521d\u5fc3\u8005\u5099\u5fd8\u9332\" target=\"_blank\">\u4ee5\u524d\u66f8\u3044\u305f\u8a18\u4e8b<\/a>\u306e\u3088\u3046\u306b\u3001Microsoft Edge\u306b\u306fInternet Explorer_Server\u30af\u30e9\u30b9\u306e\u30a6\u30a3\u30f3\u30c9\u30a6\u304c\u3042\u308a\u3001\u305d\u3053\u304b\u3089IHTMLDocument2\u7d4c\u7531\u3067DOM\u64cd\u4f5c\u3059\u308b\u3053\u3068\u304c\u3067\u304d\u307e\u3059\u3002<\/p>\n<p>\u4e0a\u8a18\u8a18\u4e8b\u3067\u306fVBA\u3067\u30b3\u30fc\u30c9\u3092\u66f8\u3044\u305f\u308f\u3051\u3067\u3059\u304c\u3001\u4eca\u56de\u306fC#\u3067\u66f8\u304d\u76f4\u3057\u3066\u307f\u3088\u3046\u3068\u601d\u3044\u307e\u3059\u3002<\/p>\n<pre class=\"brush: csharp; title: ; notranslate\" title=\"\">\/*\r\n  Microsoft Edge Automation without WebDriver.\r\n  \r\n  add references:\r\n    UIAutomationClient\r\n    UIAutomationTypes\r\n    Microsoft.mshtml\r\n  \r\n  for reference:\r\n    https:\/\/support.microsoft.com\/en-us\/kb\/249232\r\n    https:\/\/msdn.microsoft.com\/en-us\/library\/hh706902.aspx\r\n    http:\/\/blogs.msdn.com\/b\/windowsappdev\/archive\/2012\/09\/04\/automating-the-testing-of-windows-8-apps.aspx\r\n    http:\/\/stackoverflow.com\/questions\/12925748\/iapplicationactivationmanageractivateapplication-in-c\r\n    http:\/\/www.pinvoke.net\/default.aspx\/user32.EnumChildWindows\r\n    http:\/\/www.pinvoke.net\/default.aspx\/user32.FindWindowEx\r\n    http:\/\/www.pinvoke.net\/default.aspx\/user32.GetClassName\r\n    http:\/\/www.pinvoke.net\/default.aspx\/oleacc.ObjectFromLresult\r\n    http:\/\/www.pinvoke.net\/default.aspx\/user32.RegisterWindowMessage\r\n    http:\/\/www.pinvoke.net\/default.aspx\/user32.SendMessageTimeout\r\n    http:\/\/www.pinvoke.net\/default.aspx\/Enums.SendMessageTimeoutFlags\r\n*\/\r\nusing System;\r\nusing System.Text;\r\nusing System.Runtime.InteropServices;\r\nusing System.Runtime.CompilerServices;\r\nusing System.Windows.Automation;\r\nusing mshtml;\r\n\r\nnamespace EdgeAutomation\r\n{\r\n  class Program\r\n  {\r\n    public enum ACTIVATEOPTIONS : uint\r\n    {\r\n      AO_NONE = 0x00000000,\r\n      AO_DESIGNMODE = 0x00000001,\r\n      AO_NOERRORUI = 0x00000002,\r\n      AO_NOSPLASHSCREEN = 0x00000004\r\n    }\r\n    \r\n    &#x5B;Flags]\r\n    public enum SendMessageTimeoutFlags : uint\r\n    {\r\n      SMTO_NORMAL = 0x0000,\r\n      SMTO_BLOCK = 0x0001,\r\n      SMTO_ABORTIFHUNG = 0x0002,\r\n      SMTO_NOTIMEOUTIFNOTHUNG = 0x0008,\r\n      SMTO_ERRORONEXIT = 0x0020\r\n    }\r\n    \r\n    public delegate bool Win32Callback(IntPtr hWnd, IntPtr lParam);\r\n    \r\n    &#x5B;ComImport, Guid(&quot;2e941141-7f97-4756-ba1d-9decde894a3d&quot;), InterfaceType(ComInterfaceType.InterfaceIsIUnknown)]\r\n    interface IApplicationActivationManager\r\n    {\r\n      IntPtr ActivateApplication(&#x5B;In] String appUserModelId, &#x5B;In] String arguments, &#x5B;In] ACTIVATEOPTIONS options, &#x5B;Out] out UInt32 processId);\r\n      IntPtr ActivateForFile(&#x5B;In] String appUserModelId, &#x5B;In] IntPtr \/*IShellItemArray* *\/ itemArray, &#x5B;In] String verb, &#x5B;Out] out UInt32 processId);\r\n      IntPtr ActivateForProtocol(&#x5B;In] String appUserModelId, &#x5B;In] IntPtr \/* IShellItemArray* *\/itemArray, &#x5B;Out] out UInt32 processId);\r\n    }\r\n    \r\n    &#x5B;ComImport, Guid(&quot;45BA127D-10A8-46EA-8AB7-56EA9078943C&quot;)]\r\n    class ApplicationActivationManager : IApplicationActivationManager\r\n    {\r\n      &#x5B;MethodImpl(MethodImplOptions.InternalCall, MethodCodeType = MethodCodeType.Runtime)\/*, PreserveSig*\/]\r\n      public extern IntPtr ActivateApplication(&#x5B;In] String appUserModelId, &#x5B;In] String arguments, &#x5B;In] ACTIVATEOPTIONS options, &#x5B;Out] out UInt32 processId);\r\n      &#x5B;MethodImpl(MethodImplOptions.InternalCall, MethodCodeType = MethodCodeType.Runtime)]\r\n      public extern IntPtr ActivateForFile(&#x5B;In] String appUserModelId, &#x5B;In] IntPtr \/*IShellItemArray* *\/ itemArray, &#x5B;In] String verb, &#x5B;Out] out UInt32 processId);\r\n      &#x5B;MethodImpl(MethodImplOptions.InternalCall, MethodCodeType = MethodCodeType.Runtime)]\r\n      public extern IntPtr ActivateForProtocol(&#x5B;In] String appUserModelId, &#x5B;In] IntPtr \/* IShellItemArray* *\/itemArray, &#x5B;Out] out UInt32 processId);\r\n    }\r\n    \r\n    &#x5B;DllImport(&quot;user32.dll&quot;)]\r\n    &#x5B;return: MarshalAs(UnmanagedType.Bool)]\r\n    public static extern bool EnumChildWindows(IntPtr parentHandle, Win32Callback callback, IntPtr lParam);\r\n    \r\n    &#x5B;DllImport(&quot;user32.dll&quot;, SetLastError = true)]\r\n    public static extern IntPtr FindWindowEx(IntPtr hwndParent, IntPtr hwndChildAfter, string lpszClass, string lpszWindow);\r\n    \r\n    &#x5B;DllImport(&quot;user32.dll&quot;, CharSet = CharSet.Auto)]\r\n    public static extern IntPtr GetClassName(IntPtr hWnd, StringBuilder lpClassName, int nMaxCount);\r\n    \r\n    &#x5B;DllImport(&quot;oleacc.dll&quot;, PreserveSig=false)]\r\n    &#x5B;return: MarshalAs(UnmanagedType.Interface)]\r\n    public static extern object ObjectFromLresult(UIntPtr lResult, &#x5B;MarshalAs(UnmanagedType.LPStruct)] Guid refiid, IntPtr wParam);\r\n    \r\n    &#x5B;DllImport(&quot;user32.dll&quot;, SetLastError=true, CharSet=CharSet.Auto)]\r\n    public static extern uint RegisterWindowMessage(string lpString);\r\n    \r\n    &#x5B;DllImport(&quot;user32.dll&quot;, SetLastError=true, CharSet=CharSet.Auto)]\r\n    public static extern IntPtr SendMessageTimeout(IntPtr windowHandle, uint Msg, IntPtr wParam, IntPtr lParam, SendMessageTimeoutFlags flags, uint timeout, out UIntPtr result);\r\n    \r\n    public static void Main(string&#x5B;] args)\r\n    {\r\n      uint pid;\r\n      const string edgeUserModelId = &quot;Microsoft.MicrosoftEdge_8wekyb3d8bbwe!MicrosoftEdge&quot;;\r\n      const string url = &quot;https:\/\/www.bing.com\/&quot;;\r\n      \r\n      \/\/navigate to www.bing.com\r\n      ApplicationActivationManager aam = new ApplicationActivationManager();\r\n      aam.ActivateApplication(edgeUserModelId, url, ACTIVATEOPTIONS.AO_NONE, out pid);\r\n      System.Threading.Thread.Sleep(1000);\r\n      \r\n      IntPtr hEdge = IntPtr.Zero;\r\n      hEdge = FindWindowEx((IntPtr)0, (IntPtr)0, &quot;Windows.UI.Core.CoreWindow&quot;, &quot;Microsoft Edge&quot;); \/\/get the minimized Edge window \r\n      if (hEdge == IntPtr.Zero) { \/\/Edge is not minimized\r\n        AutomationElement root = AutomationElement.RootElement;\r\n        foreach (AutomationElement child in root.FindAll(TreeScope.Children, PropertyCondition.TrueCondition)) {\r\n          AutomationElement edge = child.FindFirst(TreeScope.Children, \r\n                                     new AndCondition(\r\n                                       new PropertyCondition(AutomationElement.ClassNameProperty, &quot;Windows.UI.Core.CoreWindow&quot;), \r\n                                       new PropertyCondition(AutomationElement.NameProperty, &quot;Microsoft Edge&quot;)\r\n                                     )\r\n                                   );\r\n          if (edge != null) {\r\n            hEdge = (IntPtr)edge.Current.NativeWindowHandle;\r\n            break;\r\n          }\r\n        }\r\n      }\r\n      if (hEdge != IntPtr.Zero) {\r\n        Win32Callback childProc = new Win32Callback(EnumWindow);\r\n        if (EnumChildWindows(hEdge, childProc, (IntPtr)0) == false) {\r\n          if (pid != 0) {\r\n            System.Diagnostics.Process pr = System.Diagnostics.Process.GetProcessById((int)pid);\r\n            if (pr != null) {\r\n              pr.Kill();\r\n            }\r\n          }\r\n        }\r\n      }\r\n    }\r\n    \r\n    \/\/get active HTMLDocument\r\n    public static bool EnumWindow(IntPtr handle, IntPtr pointer)\r\n    {\r\n      StringBuilder buf = new StringBuilder(255);\r\n      IntPtr ret = GetClassName(handle, buf, buf.Capacity);\r\n      if (ret != IntPtr.Zero) {\r\n        string className = buf.ToString();\r\n        if (className == &quot;Internet Explorer_Server&quot;) {\r\n          IHTMLDocument2 idoc2 = GetHTMLDocumentFromWindow(handle);\r\n          if (idoc2 != null) {\r\n            try {\r\n              \/\/find the search box and query for 'Microsoft Edge'\r\n              HTMLDocument doc = (HTMLDocument)idoc2;\r\n              HTMLInputElement searchBox = (HTMLInputElement)doc.getElementById(&quot;sb_form_q&quot;);\r\n              HTMLInputElement searchSubmit = (HTMLInputElement)doc.getElementById(&quot;sb_form_go&quot;);\r\n              searchBox.value = &quot;Microsoft Edge&quot;;\r\n              searchSubmit.click();\r\n              \r\n              \/\/wait for search result\r\n              while (doc.readyState != &quot;complete&quot;) {\r\n                System.Threading.Thread.Sleep(100);\r\n              }\r\n              Console.WriteLine(doc.title + &quot;, &quot; + doc.url); \/\/get the document title &amp; url\r\n            } catch (Exception e) {\r\n              Console.WriteLine(e);\r\n              throw;\r\n            }\r\n          }\r\n          return false;\r\n        }\r\n      }\r\n      return true;\r\n    }\r\n    \r\n    public static IHTMLDocument2 GetHTMLDocumentFromWindow(IntPtr hWnd)\r\n    {\r\n      UIntPtr lRes;\r\n      IHTMLDocument2 doc = null;\r\n      Guid IID_IHTMLDocument = new Guid(&quot;626FC520-A41E-11CF-A731-00A0C9082637&quot;);\r\n      uint nMsg = RegisterWindowMessage(&quot;WM_HTML_GETOBJECT&quot;);\r\n      if (nMsg != 0) {\r\n        SendMessageTimeout(hWnd, nMsg, (IntPtr)0, (IntPtr)0, SendMessageTimeoutFlags.SMTO_ABORTIFHUNG, 1000, out lRes);\r\n        if (lRes != UIntPtr.Zero) {\r\n          doc = (IHTMLDocument2)ObjectFromLresult(lRes, IID_IHTMLDocument, (IntPtr)0);\r\n        }\r\n      }\r\n      return doc;\r\n    }\r\n  }\r\n}<\/pre>\n<p><a href=\"\/\/www.ka-net.org\/blog\/wp-content\/uploads\/EdgeAutomationWithoutWebDriver_01.gif\"><img loading=\"lazy\" decoding=\"async\" src=\"\/\/www.ka-net.org\/blog\/wp-content\/uploads\/EdgeAutomationWithoutWebDriver_01.gif\" alt=\"EdgeAutomationWithoutWebDriver_01\" width=\"835\" height=\"668\" class=\"alignnone size-full wp-image-6171\" \/><\/a><\/p>\n<p>\u66f8\u304d\u76f4\u3059\u3064\u3044\u3067\u306b\u3001Edge\u306e\u8d77\u52d5\u90e8\u5206\u3092\u300c<a href=\"\/\/www.ka-net.org\/blog\/?p=6161\" title=\"Microsoft Edge\u3092\u8d77\u52d5\u3059\u308bC#\u30b3\u30fc\u30c9 | \u521d\u5fc3\u8005\u5099\u5fd8\u9332\" target=\"_blank\">Microsoft Edge\u3092\u8d77\u52d5\u3059\u308bC#\u30b3\u30fc\u30c9<\/a>\u300d\u3067\u7d39\u4ecb\u3057\u3066\u3044\u308b\u3001<span style=\"color: #ff0000; font-weight: bold;\">IApplicationActivationManager::ActivateApplication<\/span>\u30e1\u30bd\u30c3\u30c9\u3092\u4f7f\u3046\u65b9\u6cd5\u306b\u3057\u3066\u307f\u307e\u3057\u305f\u3002<\/p>\n<p>(\u30b3\u30fc\u30c9\u304c\u9577\u304f\u306a\u3063\u3066\u3057\u307e\u3063\u305f\u306e\u3067\u3001\u3082\u3046\u3061\u3087\u3063\u3068\u30b9\u30c3\u30ad\u30ea\u3055\u305b\u3088\u3046\u3068\u601d\u3063\u305f\u306e\u3067\u3059\u304c\u3001\u9014\u4e2d\u3067\u9762\u5012\u304f\u3055\u304f\u306a\u3063\u305f\u306e\u3067\u6b62\u3081\u307e\u3057\u305f\u3002<br \/>\n\u3053\u3093\u306a\u30b3\u30fc\u30c9\u3067\u3059\u304c\u3001\u3069\u306a\u305f\u304b\u306e\u53e9\u304d\u53f0\u306b\u3067\u3082\u306a\u308c\u3070\u5e78\u3044\u3067\u3059\u3002)<\/p>\n","protected":false},"excerpt":{"rendered":"\u4ee5\u524d\u66f8\u3044\u305f\u8a18\u4e8b\u306e\u3088\u3046\u306b\u3001Microsoft Edge\u306b\u306fInternet Explorer_Server\u30af\u30e9\u30b9\u306e\u30a6\u30a3\u30f3\u30c9\u30a6\u304c\u3042\u308a\u3001\u305d\u3053\u304b\u3089IHTMLDocument2\u7d4c\u7531\u3067DOM\u64cd\u4f5c\u3059\u308b\u3053\u3068\u304c\u3067\u304d\u307e\u3059\u3002 \u4e0a\u8a18\u8a18\u4e8b\u3067\u306fVB [&hellip;]","protected":false},"author":1,"featured_media":7115,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_jetpack_memberships_contains_paid_content":false,"footnotes":"","jetpack_publicize_message":"Microsoft #Edge Automation without #WebDriver ( C# Code ).","jetpack_publicize_feature_enabled":true,"jetpack_social_post_already_shared":true,"jetpack_social_options":{"image_generator_settings":{"template":"highway","default_image_id":0,"font":"","enabled":false},"version":2}},"categories":[132,25,133],"tags":[],"class_list":["post-6170","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-windows10","category-windows","category-microsoft-edge"],"aioseo_notices":[],"jetpack_publicize_connections":[],"jetpack_featured_media_url":"https:\/\/www.ka-net.org\/blog\/wp-content\/uploads\/eyecatch-MicrosoftEdge.png","jetpack_shortlink":"https:\/\/wp.me\/p4UZZr-1Bw","jetpack_sharing_enabled":true,"_links":{"self":[{"href":"https:\/\/www.ka-net.org\/blog\/index.php?rest_route=\/wp\/v2\/posts\/6170","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.ka-net.org\/blog\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.ka-net.org\/blog\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.ka-net.org\/blog\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.ka-net.org\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=6170"}],"version-history":[{"count":1,"href":"https:\/\/www.ka-net.org\/blog\/index.php?rest_route=\/wp\/v2\/posts\/6170\/revisions"}],"predecessor-version":[{"id":6172,"href":"https:\/\/www.ka-net.org\/blog\/index.php?rest_route=\/wp\/v2\/posts\/6170\/revisions\/6172"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.ka-net.org\/blog\/index.php?rest_route=\/wp\/v2\/media\/7115"}],"wp:attachment":[{"href":"https:\/\/www.ka-net.org\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=6170"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.ka-net.org\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=6170"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.ka-net.org\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=6170"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}