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

C# TestStack.White - Unable to pinpoint to the button I want

我想打开一个 windows 应用程序,然后按工具栏中的 "Save Screen" 按钮:

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

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

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();

一般可以使用原生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) 从列表中找到您的按钮并进入点

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

5) 点击点:

Mouse.Instance.Click(clickablePoint);

检查代码,因为我是凭记忆写的,没有 IDE :)