C# なので powershell へ要変更ですが
ダウンロードフォルダ設定及び自動操作中のinfobarを閉じる
UiAutomation のサンプルです。
public static void SetEdgeDownloadFolder(string windowNamePattern, string downloadFolderPath)
{
var rootElement = AutomationElement.RootElement;
var edgeElement = FindElementByClassNameAndNameReg(rootElement, "Chrome_WidgetWin_1", windowNamePattern);
if (edgeElement is not null)
{
var dialogElement = FindElementByClassName(edgeElement, TreeScope.Children, "#32770");
if (dialogElement is not null)
{
var editElement = FindElementByClassName(dialogElement, TreeScope.Children, "Edit");
if (editElement is not null)
SetValue(editElement, downloadFolderPath);
var selectElement = FindElementByClassNameAndName(dialogElement, "Button", "フォルダーの選択");
if (selectElement is not null)
Invoke(selectElement);
}
}
}
public static void CloseInfoBar(string windowNamePattern)
{
var rootElement = AutomationElement.RootElement;
var edgeElement = FindElementByClassNameAndNameReg(rootElement, "Chrome_WidgetWin_1", windowNamePattern);
if (edgeElement is not null)
{
var infoBarContainerElement = FindElementByClassName(edgeElement, TreeScope.Descendants, "InfoBarContainerView");
if (infoBarContainerElement is not null)
{
var cloaseButtonElement = FindElementByClassName(infoBarContainerElement, TreeScope.Descendants, "ImageButton");
if (cloaseButtonElement is not null)
{
Invoke(cloaseButtonElement);
}
}
}
}
private static AutomationElement? FindElementByClassNameAndNameReg(AutomationElement srcElement, string className, string namePattern)
{
AutomationElement? result = null;
var elements = srcElement.FindAll(TreeScope.Element | TreeScope.Children,
new PropertyCondition(AutomationElement.ClassNameProperty, className))
.Cast<AutomationElement>();
Regex reg = new Regex(namePattern);
foreach (var element in elements)
{
if (reg.IsMatch(element.Current.Name))
{
result = element;
break;
}
}
return result;
}
private static AutomationElement FindElementByClassName(AutomationElement srcElement, TreeScope treeScope, string className)
=> srcElement.FindFirst(TreeScope.Element | treeScope, new PropertyCondition(AutomationElement.ClassNameProperty, className));
private static AutomationElement? FindElementByClassNameAndName(AutomationElement srcElement, string className, string name)
{
return srcElement.FindAll(TreeScope.Element | TreeScope.Children,
new PropertyCondition(AutomationElement.ClassNameProperty, className))
.Cast<AutomationElement>()
.Where(x => x.Current.Name.Equals(name))
.First();
}
private static void SetValue(AutomationElement srcElement, string elementText)
{
var valuePattern = srcElement.GetCurrentPattern(ValuePattern.Pattern) as ValuePattern;
if (valuePattern is not null)
valuePattern.SetValue(elementText);
}
private static void Invoke(AutomationElement srcElement)
{
var invokePattern = srcElement.GetCurrentPattern(InvokePattern.Pattern) as InvokePattern;
if (invokePattern is not null)
invokePattern.Invoke();
}