I am using the code similar to this one https://learn.microsoft.com/en-us/windows/win32/winauto/uiauto-howto-find-ui-elements#finding-an-element-by-name for getting specific element within a window and it doesn't work if my element is inside iframe which is inside electron app.
However this changes if I run insights and manually inspect my electron application (Slack, Teams etc.). After that my code execution finds exact element within the iframe.
EDIT:
int main(int argc, char *argv[])
{
HRESULT hr;
int ret = 0;
IUIAutomationElement *pTargetElement = NULL;
CoInitializeEx(NULL, COINIT_MULTITHREADED);
IUIAutomationElement *pRootElement;
std::string elementName = "Missed";
CComBSTR temp(elementName.c_str());
std::string inputWindowTitle = "Microsoft Teams";
CA2T wt (inputWindowTitle.c_str());
HWND hwnd = ::FindWindow(NULL, wt);
IUIAutomation *pAutomation = NULL;
hr = CoCreateInstance(__uuidof(CUIAutomation), NULL, CLSCTX_INPROC_SERVER, __uuidof(IUIAutomation), (void **)&pAutomation);
if (FAILED(hr) || pAutomation == NULL)
{
ret = 1;
goto cleanup;
}
PROPERTYID pPIDProperties[] = {UIA_BoundingRectanglePropertyId};
IUIAutomationCondition *pCondition;
VARIANT varProperty;
VariantInit(&varProperty);
varProperty.vt = VT_BSTR;
varProperty.bstrVal = temp;
SetForegroundWindow(hwnd);
SetFocus(hwnd);
SetActiveWindow(hwnd);
if (SUCCEEDED(pAutomation->ElementFromHandle(hwnd, &pRootElement)))
{
if (SUCCEEDED(pAutomation->CreatePropertyCondition(UIA_NamePropertyId, varProperty, &pCondition)))
{
if (SUCCEEDED(pRootElement->FindFirst(TreeScope_Descendants, pCondition, &pTargetElement)))
{
if (pTargetElement != NULL)
{
BSTR nn;
hr = pTargetElement->get_CurrentName(&nn);
wprintf(L">> ELEMENT FOUND (%s)! ", nn);
} else {
wprintf(L">> ELEMENT IS NULL");
goto cleanup;
}
} else {
wprintf(L">> ELEMENT NOT FOUND! ");
goto cleanup;
}
} else {
wprintf(L">> SOMETHING WRONG WITH CONDITION ");
goto cleanup;
}
}
else {
wprintf(L">> NO WINDOW ");
goto cleanup;
}
cleanup:
if (pTargetElement != NULL)
pTargetElement->Release();
if (pAutomation != NULL)
pAutomation->Release();
CoUninitialize();
return ret;
}
How should I approach this correctly and why after Accessibility Insights interaction my element can be found, is there some caching behind this?