简体   繁体   English

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

[英]Using UI Automation to click a button in an application

I'm trying to use UI Automation to click a simple button in a 3rd party application. 我正在尝试使用UI自动化单击第三方应用程序中的简单按钮。 I have initialized the IUIAutomation object and retrieved an IUIAutomationElement by using the ElementFromHandle() function with the handle of the 3rd party application window. 我已经初始化IUIAutomation对象,并通过使用ElementFromHandle()函数和第三方应用程序窗口的句柄来检索IUIAutomationElement。

But I really can't figure out how to use this IUIAutomationElement to find the button and issue a leftbutton click on it.. 但是我真的不知道如何使用此IUIAutomationElement来查找按钮并发出左键单击。

This is my code so far: 到目前为止,这是我的代码:

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

} }

Hope someone can help me how to continue from here to actually click the button 希望有人可以帮助我如何从这里继续以实际单击按钮

You may find it simpler to use TestStack.White (also available from NuGet) to do this kind of thing. 您可能会发现使用TestStack.White (也可以从NuGet获得)更简单地执行这种操作。 It wraps up a lot of UIAutomation to make it easier to write tests that drive Windows applications. 它包装了很多UIAutomation,以使编写驱动Windows应用程序的测试变得更加容易。

To find the target button you can use "UIVerify" or "inspect" from the Windows 10 SDK which lets you view the UI automation tree for all windows on the desktop. 要找到目标按钮,您可以使用Windows 10 SDK中的“ UIVerify”或“ inspect”,通过它可以查看桌面上所有窗口的UI自动化树。 Once you locate the button of interest you can note its AutomationId or other properties to use as search parameters to one of TestStack.White 's search commands or an IUIAutomationTreeWalker instance. 找到感兴趣的按钮后,您可以注意到其AutomationId或其他属性,以用作TestStack.White的搜索命令或IUIAutomationTreeWalker实例之一的搜索参数。

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

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