前提・実現したいこと
お世話になります。
本ソースはこちらを参考にWindowsフォームで検証中です。
https://teratail.com/questions/88664
発生している問題・エラーメッセージ
①タブの情報が取得できない ②Edgeが最小化されている場合そもそも取得できない
該当のソースコード
C#
1using System; 2using System.Collections.Generic; 3using System.ComponentModel; 4using System.Data; 5using System.Drawing; 6using System.Linq; 7using System.Text; 8using System.Threading.Tasks; 9using System.Windows.Forms; 10using UIAutomationClient; 11 12namespace GetEdgeUrl 13{ 14 public partial class frmMain : Form 15 { 16 public frmMain() 17 { 18 InitializeComponent(); 19 } 20 21 private void btnTest_Click(object sender, EventArgs e) 22 { 23 MessageBox.Show("Hello World!!", btnTest.Text, MessageBoxButtons.OK, MessageBoxIcon.Information); 24 } 25 26 private void btnExecute_Click(object sender, EventArgs e) 27 { 28 GetTrialEdgeUrl02(); 29 30 } 31 32 private void GetTrialEdgeUrl02() 33 { 34 string CLSID_CUIAutomation = "ff48dba4-60ef-4201-aa87-54103eef594e"; 35 Type type = Type.GetTypeFromCLSID(Guid.Parse(CLSID_CUIAutomation)); 36 IUIAutomation automation = Activator.CreateInstance(type) as IUIAutomation; 37 38 IUIAutomationElement root = automation.GetRootElement(); 39 IUIAutomationTreeWalker walker = automation.RawViewWalker; 40 41 IUIAutomationElement edge = null; 42 //IUIAutomationElementArray edgeAry = null; 43 44 edge = walker.GetFirstChildElement(root); 45 while (edge != null) 46 { 47 string name = edge.CurrentName; 48 if (!string.IsNullOrEmpty(name) && name.EndsWith(" - Microsoft Edge")) 49 { 50 //break; 51 //Edge配下のタイトルが取得できそうなエレメントを取得 52 IUIAutomationElement m_tabContentDCompVisualElement = null; 53 { 54 const int UIA_AutomationIdPropertyId = 30011; 55 var cond = automation.CreatePropertyCondition(UIA_AutomationIdPropertyId, 56 "m_tabContentDCompVisualElement"); 57 m_tabContentDCompVisualElement = edge.FindFirst(TreeScope.TreeScope_Subtree, cond); 58 59 } 60 61 if (m_tabContentDCompVisualElement == null) 62 { 63 //MessageBox.Show("Edgeが起動していません。", btnExecute.Text, MessageBoxButtons.OK, MessageBoxIcon.Warning); 64 return; 65 } 66 67 //タイトル 68 string title = m_tabContentDCompVisualElement.CurrentName; 69 70 //Edge配下のURLが取得できそうなエレメントを取得 71 IUIAutomationElement ie_server; 72 { 73 const int UIA_ClassNamePropertyId = 30012; 74 var cond = automation.CreatePropertyCondition(UIA_ClassNamePropertyId, "Internet Explorer_Server"); 75 ie_server = edge.FindFirst(TreeScope.TreeScope_Subtree, cond); 76 77 } 78 79 if (ie_server == null) 80 { 81 return; 82 } 83 84 //URL 85 string url = ie_server.CurrentName; 86 87 StringBuilder MsgLine = new StringBuilder(); 88 MsgLine.Append("Title:"); 89 MsgLine.AppendLine(title); 90 MsgLine.Append("Url:"); 91 MsgLine.AppendLine(url); 92 93 MessageBox.Show(MsgLine.ToString(), btnExecute.Text, MessageBoxButtons.OK, MessageBoxIcon.Information); 94 95 } 96 IUIAutomationElement next = walker.GetNextSiblingElement(edge); 97 edge = next; 98 } 99 if (edge == null) 100 { 101 MessageBox.Show("Edgeが起動していません。", btnExecute.Text, MessageBoxButtons.OK, MessageBoxIcon.Warning); 102 return; 103 } 104 105 106 107 //Console.WriteLine(title); 108 //Console.WriteLine(url); 109 //Console.ReadKey(); 110 } 111 } 112}
試したこと
IUIAutomationElementArrayを使えばいいのかなと思ったのですが、
うまくいかず…
補足情報(FW/ツールのバージョンなど)
Microsoft Visual Studio Professional 2017
Version 15.6.5
Microsoft .NET Framework
Version 4.7.02556
Microsoft Edge 41.16299.248.0
回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。