简体   繁体   English

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

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

I want to open a windows application, and press the "Save Screen" button from the toolbar: 我想打开Windows应用程序,然后从工具栏中按“保存屏幕”按钮:

保存屏幕按钮

Using Spy++ from Visual Studio, I am able to get the following properties for this toolbar: 使用Visual Studio中的Spy ++,我可以为此工具栏获取以下属性:

常规选项卡类标签

With above information, I tried this: 利用以上信息,我尝试了以下操作:

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

With the code above, the program was able to locate the toolbar, but it would simply click at the middle of the toolbar, which is 9th icon from the left (the "Print Preview" button 使用上面的代码,程序可以找到工具栏,但是只需单击工具栏中间的位置,该工具栏是左侧第9个图标(“打印预览”按钮 打印预览 ). )。

I also tried SearchCriteria.ByText("Save Screen") but that gave me error. 我也尝试了SearchCriteria.ByText("Save Screen")但这给了我错误。

So how can make the mouseclick to be at the button I want? 那么如何使鼠标单击位于我想要的按钮上呢? Is there a mouseclick coordinate offset that I can adjust? 我可以调整mouseclick坐标偏移吗?

The problem is that all buttons on your toolbar have same ClassName - ToolbarWindows32. 问题在于工具栏上的所有按钮都具有相同的ClassName -ToolbarWindows32。

Try to find all buttons on your toolbar and get yours by index. 尝试找到工具栏上的所有按钮,然后按索引获取按钮。

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

Generally, you can use native UI Automation: 通常,您可以使用本机UI自动化:

1) Find the toolbar: 1)找到工具栏:

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

2) Get its Automation element for toolbar: 2)获取工具栏的Automation元素:

AutomationElement toolbarAe = toolbar.AutomationElement;

3) Get all children for this AutomationElement. 3)获取此AutomationElement的所有子级。

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

4) Find your button from the list and get in Point 4)从列表中找到您的按钮并进入Point

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

5) Click on point: 5)点击点:

Mouse.Instance.Click(clickablePoint);

Check the code cause I wrote it from memory without IDE :) 检查代码原因,因为我是从没有IDE的内存中写出来的:)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM