gpt4 book ai didi

C# TestStack.White - 无法精确定位到我想要的按钮

转载 作者:太空宇宙 更新时间:2023-11-03 21:15:12 25 4
gpt4 key购买 nike

我想打开一个 Windows 应用程序,然后按工具栏中的“保存屏幕”按钮:

Save Screen Button

使用 Visual Studio 中的 Spy++,我能够获得此工具栏的以下属性:

General Tab Class Tab

根据以上信息,我尝试了这个:

Application app = Application.Attach(7492); //The process was manually opened
Window window = app.GetWindow("Scope Link - Virtual Instrument", InitializeOption.Nocache);
SearchCriteria sc = SearchCriteria.ByClassName("ToolbarWindows32"); //AutomationID is 59392
var saveBtn = window.Get(sc);
saveBtn.Click();

使用上面的代码,程序能够找到工具栏,但它只需单击工具栏的中间,即从左边算起的第 9 个图标(“打印预览”按钮 Print Preview)。

我也试过 SearchCriteria.ByText("Save Screen") 但这给了我错误。

那么怎样才能使鼠标点击在我想要的按钮上呢?是否有我可以调整的鼠标点击坐标偏移量?

最佳答案

问题是工具栏上的所有按钮都有相同的类名 - ToolbarWindows32。

尝试找到工具栏上的所有按钮并按索引获取您的按钮。

toolbar.GetMultiple(SeacrhCriteria.ByControlType(ControlType.Button))[INDEX].Click();

通常,您可以使用 native UI 自动化:

1) 找到工具栏:

var toolbar = window.Get(SearchCriteria.ByClassName("ToolbarWindows32"));

2) 获取工具栏的自动化元素:

AutomationElement toolbarAe = toolbar.AutomationElement;

3) 获取此 AutomationElement 的所有子项。

var listAeChildren = toolbarAe.findAll(Treescope.Children, new PropertyCondition(AutomationElement.ControltypeProperty, ControlType.Button));

4) 从列表中找到你的按钮并进入Point

var clickablePoint = new Point();
foreach(AutomationElement button : listAeChildren)
{
if(button.Current.Name.Equals("Save Screen"))
{
clickablePoint = button.GetClickablePoint();
}
}

5) 点击点:

Mouse.Instance.Click(clickablePoint);

检查代码,因为我是在没有 IDE 的情况下从内存中编写的:)

关于C# TestStack.White - 无法精确定位到我想要的按钮,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34849784/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com