Skip to content

Instantly share code, notes, and snippets.

@iskam9822
Last active January 15, 2017 17:20
UI Automationを使用したテキストボックスの値を取得・変更
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Windows.Automation;
namespace UIAutomation
{
class Program
{
private static readonly string PROCESS_NAME = @"実行プログラム";
private static readonly string FILE_NAME = @"ファイルフルパス";
private static readonly int DEFAULT_WAIT_TIME = 3000;
static void Main(string[] args)
{
Thread.Sleep(DEFAULT_WAIT_TIME);
Process process = Process.Start(PROCESS_NAME, FILE_NAME);
try
{
Thread.Sleep(DEFAULT_WAIT_TIME);
mainForm = AutomationElement.FromHandle(process.MainWindowHandle); //起動したWindowのハンドルを取得
mainTable = FindElementsByControlType(mainForm,ControlType.Edit); //取得したフォーム全体からControlTypeがEditのものを取得
//AutomationElementCollectionで取得したものは要素で取得可能。PatternにValluePatternを指定して取得
ValuePattern txtboxName = (ValuePattern)mainTable[6].GetCurrentPattern(ValuePattern.Pattern);
var txtValue = txtboxName.Current.Value; //テキストボックスの中身
txtboxName.SetValue("test"); //中身を変更したい場合
}
finally
{
//process.CloseMainWindow();
}
}
// 指定したControlType属性に一致する要素をすべて返します
private static AutomationElementCollection FindElementsByControlType(AutomationElement rootElement, ControlType controlType)
{
return rootElement.FindAll(
TreeScope.Element | TreeScope.Descendants,
new PropertyCondition(AutomationElement.ControlTypeProperty, controlType));
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment