使用 UI 自动化单击应用程序中的按钮

Using UI Automation to click a button in an application

本文关键字:按钮 应用程序 单击 UI 自动化 使用      更新时间:2023-10-16

我正在尝试使用 UI 自动化单击第三方应用程序中的简单按钮。我已经初始化了 IUIAutomation 对象,并通过使用 ElementFromHandle() 函数和第三方应用程序窗口的句柄检索了 IUIAutomationElement。

但是我真的不知道如何使用这个IUIAutomationElement来查找按钮并发出左键单击它。

这是我到目前为止的代码:

void Control::clickButton()
{
    for (std::list<Window>::iterator i = mainDetector.getWindowList().begin(); i != mainDetector.getWindowList().end(); ++i)
{
    if (i->getTitle().find("PokerStars Lobby") != std::string::npos)
    {
        parentWindowHandle = (HWND)i->getHandle();
    }
}
InitializeUIAutomation(iUiAutomation);
(*iUiAutomation)->ElementFromHandle(parentWindowHandle, iUiAutomationElement);

}

希望有人能帮助我如何从这里继续实际单击按钮

你可能会

发现使用TestStack.White(也可以从NuGet获得)来做这种事情更简单。它总结了许多UIAutomation,以便更轻松地编写驱动Windows应用程序的测试。

要查找目标按钮,您可以使用Windows 10 SDK中的"UIVerify"或"检查",它允许您查看桌面上所有窗口的UI自动化树。找到感兴趣的按钮后,您可以记下其 AutomationId 或其他属性,以用作TestStack.White搜索命令之一或IUIAutomationTreeWalker实例的搜索参数。