C# 如何使用windows AutomationElement制作点击按钮
在这篇文章之后 我正试图打开一个应用程序并按下一个按钮。这就是我想要的C# 如何使用windows AutomationElement制作点击按钮,c#,asp.net,.net,automation,automated-tests,C#,Asp.net,.net,Automation,Automated Tests,在这篇文章之后 我正试图打开一个应用程序并按下一个按钮。这就是我想要的 public RecordProgram() { ProcessStartInfo psi = new ProcessStartInfo(@"C:\MouseController.exe", @"C:\test1.mcd"); psi.UseShellExecute = false; _calculatorProcess = Process.Start(psi);
public RecordProgram()
{
ProcessStartInfo psi = new ProcessStartInfo(@"C:\MouseController.exe", @"C:\test1.mcd");
psi.UseShellExecute = false;
_calculatorProcess = Process.Start(psi);
int ct = 0;
do
{
_calculatorAutomationElement = AutomationElement.RootElement.FindFirst(TreeScope.Children, new PropertyCondition(AutomationElement.NameProperty, "MouseController (1,0x)"));
++ct;
Thread.Sleep(100);
}
while (_calculatorAutomationElement == null && ct < 50);
if (_calculatorAutomationElement == null)
{
throw new InvalidOperationException("Calculator must be running");
}
_resultTextBoxAutomationElement = _calculatorAutomationElement.FindFirst(TreeScope.Element, new PropertyCondition(AutomationElement.AutomationIdProperty, "920388"));
if (_resultTextBoxAutomationElement == null)
{
throw new InvalidOperationException("Could not find result box");
}
GetInvokePattern(GetFunctionButton(Functions.Clear)).Invoke();
}
对_calculatorAutomationeElement.FindFirst()的调用不应该传入TreeScope.Children而不是TreeScope.Element吗?(假设您要查找的button元素是app window元素的直接子元素。)在您执行此操作时,通过传入TreeScope.element,UIA将只查看自动ID为920388的元素的_calculatorAutomationElement本身 谢谢
Guy对_calculatorAutomationeElement.FindFirst()的调用不应该传入TreeScope.Children而不是TreeScope.Element吗?(假设您要查找的button元素是app window元素的直接子元素。)在您执行此操作时,通过传入TreeScope.element,UIA将只查看自动ID为920388的元素的_calculatorAutomationElement本身 谢谢
Guy为了说明我在上面的评论,其中提到Run dlg的Browse按钮是通过其AutomationId访问Win32按钮的一个示例,我刚刚编写了下面的代码来访问Browse按钮。代码使用了Windows UIA API的托管包装器,我使用tlbimp.exe工具生成了该包装器,但我希望采用与.NET UIA API类似的方法也能很好地工作 因此,对于上面显示的MouseController UI,尝试将行更改为
_resultTextBoxAutomationElement = _calculatorAutomationElement.FindFirst(
TreeScope.Children, new PropertyCondition
(AutomationElement.AutomationIdProperty, "2296138"));
(我假设Inspect SDK工具确实显示“开始播放”按钮的AutomationId为“2296138”。)
谢谢
家伙
为了说明我在上面的评论,其中提到运行dlg的浏览按钮是通过其AutomationId访问Win32按钮的一个示例,我刚刚编写了下面的代码来访问浏览按钮。代码使用了Windows UIA API的托管包装器,我使用tlbimp.exe工具生成了该包装器,但我希望采用与.NET UIA API类似的方法也能很好地工作 因此,对于上面显示的MouseController UI,尝试将行更改为
_resultTextBoxAutomationElement = _calculatorAutomationElement.FindFirst(
TreeScope.Children, new PropertyCondition
(AutomationElement.AutomationIdProperty, "2296138"));
(我假设Inspect SDK工具确实显示“开始播放”按钮的AutomationId为“2296138”。)
谢谢
家伙
不使用TreeScope.Childer也不使用TreeScope.Degenants您正在查找AutomationId属性为920388的元素。那个号码是从哪里来的?如果我将Inspect SDK工具指向Win32 Run对话框,我可以看到对话框中的Browse按钮的AutomationId为12288。如果我将Spy工具指向同一个按钮,我会看到它的控件ID为0x3000(即12288)。因此,Win32 UI框架会自动将控件ID公开为UIA AutomationId。鉴于上面的图像显示控件ID为0x23094A,如果查找AutomationId为2296138的元素,会发生什么情况?不使用TreeScope.Childer也不使用TreeScope.Degenants您正在查找AutomationId属性为920388的元素。那个号码是从哪里来的?如果我将Inspect SDK工具指向Win32 Run对话框,我可以看到对话框中的Browse按钮的AutomationId为12288。如果我将Spy工具指向同一个按钮,我会看到它的控件ID为0x3000(即12288)。因此,Win32 UI框架会自动将控件ID公开为UIA AutomationId。鉴于上面的图像显示控件ID为0x23094A,如果查找AutomationId为2296138的元素会发生什么情况?我可以在哪里找到IUIAutomation的参考?有关IUIAutomation的详细信息,请访问。有关Windows UIA API的其他客户端接口的详细信息,请访问。我可以在哪里找到IUIAutomation的参考?有关IUIAutomation的详细信息,请访问。有关Windows UIA API的其他客户端接口的详细信息,请参阅。
IUIAutomation uiAutomation = new CUIAutomation();
IUIAutomationElement rootElement = uiAutomation.GetRootElement();
int propertyIdName = 30005; // UIA_NamePropertyId
// First find the Run dlg, which is a direct child of the root element.
// For this test, assume there's only one element whose title is "Run"
// beneath the root. Note! This only works in English UI.
IUIAutomationCondition conditionName =
uiAutomation.CreatePropertyCondition(
propertyIdName, "Run");
IUIAutomationElement wndElement = rootElement.FindFirst(
TreeScope.TreeScope_Children, conditionName);
if (wndElement != null)
{
// Ok, we have the Run dialog. Now find the Browse button through its AutomationId.
int propertyAutomationId = 30011; // UIA_AutomationIdPropertyId
// Using the Inspect SDK tool, I could see that the AutomationId of
// the Browse button is "12288".
IUIAutomationCondition conditionAutomationId =
uiAutomation.CreatePropertyCondition(
propertyAutomationId, "12288");
// Get the name of the button cached when we find the button, so that
// we don't have to make a cross-process call later to get the name.
IUIAutomationCacheRequest cacheRequestName = uiAutomation.CreateCacheRequest();
cacheRequestName.AddProperty(propertyIdName);
IUIAutomationElement btnElement = wndElement.FindFirstBuildCache(
TreeScope.TreeScope_Children, conditionAutomationId, cacheRequestName);
if (btnElement != null)
{
// Let's see the name now...
MessageBox.Show(btnElement.CachedName);
}
}