大雑把に言えばUIAutomationを使って、ほしい要素(エレメント)を探して取得して、操作する という感じ。
IUIAutomationConditionで探す要素を設定して
hogeElement.FindAll()でIUIAutomationConditionを指定して要素を探索。
見つかった要素が塊で取得できるのでループでバラしながら探す。
という流れ
・参照の追加でUIAutomationClientを追加

・操作したい要素のConttoleTypeなど、情報を調べるには「Inspent」を使う

...
// EdgeのアドレスバーのURL取得
int UIA_ControlTypePropertyId = 30003;
IUIAutomation uiAutomation = new CUIAutomation8();
IUIAutomationElement rootElement = uiAutomation.GetRootElement();
UIAutomationClient.IUIAutomationElement elmEdge;
string eURL = "";
{
int UIA_WindowControlTypeId = 50032;
IUIAutomationCondition condition =
uiAutomation.CreatePropertyCondition(
UIA_ControlTypePropertyId, UIA_WindowControlTypeId
);
UIAutomationClient.IUIAutomationElementArray aryWindowControls;
aryWindowControls = rootElement.FindAll(TreeScope.TreeScope_Subtree, condition);
elmEdge = null;
for (int i = 0; i < aryWindowControls.Length; i++)
{
string name = aryWindowControls.GetElement(i).CurrentName;
string classname = aryWindowControls.GetElement(i).CurrentClassName;
name = name.ToLower();
if (1 <= name.IndexOf("microsoft edge"))
{
elmEdge = aryWindowControls.GetElement(i);
int UIA_EditControlTypeId = 50004;
IUIAutomationCondition condition2 =
uiAutomation.CreatePropertyCondition(
UIA_ControlTypePropertyId, UIA_EditControlTypeId
);
UIAutomationClient.IUIAutomationElementArray aryWindowControls2;
aryWindowControls2 = elmEdge.FindAll(TreeScope.TreeScope_Subtree, condition2);
for (int i2 = 0; i2 < aryWindowControls2.Length; i2++)
{
name = aryWindowControls2.GetElement(i2).CurrentName;
if (name == "アドレスと検索バー")
{
int UIA_ValueValuePropertyId = 30045;
eURL = aryWindowControls2.GetElement(i2).GetCurrentPropertyValue(UIA_ValueValuePropertyId);
break;
}
}
break;
}
}
}
....
// Edgeの最後のタブを閉じる
int last_iptn = -1;
if (elmEdge != null)
{
int UIA_ButtonControlTypeId = 50000;
IUIAutomationCondition condition3 =
uiAutomation.CreatePropertyCondition(
UIA_ControlTypePropertyId, UIA_ButtonControlTypeId
);
UIAutomationClient.IUIAutomationElementArray aryWindowControls3;
aryWindowControls3 = elmEdge.FindAll(TreeScope.TreeScope_Subtree, condition3);
for (int i3 = 0; i3 < aryWindowControls3.Length; i3++)
{
string name = aryWindowControls3.GetElement(i3).CurrentName;
if (name == "タブを閉じる")
{
last_iptn = i3;
}
}
if (last_iptn != -1)
{
IUIAutomationInvokePattern iptn;
int UIA_InvokePatternId = 10000;
iptn = aryWindowControls3.GetElement(last_iptn).GetCurrentPattern(UIA_InvokePatternId);
iptn.Invoke();
}
}