关于 UIAutomation 的问题,谢谢帮忙!
最近在研究怎么获取IE里面的内容,如果想按元素类型进行分类,比如 编辑框、文本、超级链接等。
可是在打开百度首页测试时发现,获取编辑框类型(UIA_EditControlTypeId)控件时,竟然把文本类型的元素也当做编辑框类型返回来了,然后 我用微软工具 Inspect object 去查看了下,果然还真是“可编辑文本”类型,但是在UI SPY工具上看却是文本类型(UIA_TextControlTypeId),这个让人很迷惑.. 本人机器是WIN7 64位系统,安装过 Microsoft Windows SDK v7.0 。
当时的情况就是这样.. 求高手指点。。谢谢!
方法如下:
IUIAutomation* _pUIAutomation;
if (SUCCEEDED(hr))
{
hr = CoCreateInstance(CLSID_CUIAutomation, NULL, CLSCTX_INPROC_SERVER, IID_PPV_ARGS(&_pUIAutomation));
}
void CIEUIADlg::OnBnClickedOk()
{
HWND hWndBrowser = ::FindWindow(_T("IEFrame"), NULL);
IUIAutomationElement *pElementBrowser = NULL;
HRESULT hr = _pUIAutomation->ElementFromHandle((UIA_HWND)hWndBrowser, &pElementBrowser);
IUIAutomationCondition *pConditionControlView = NULL;
hr = _pUIAutomation->get_ControlViewCondition(&pConditionControlView);
IUIAutomationCondition *pCondition = NULL;
VARIANT varProp;
varProp.vt = VT_I4;
//varProp.lVal = UIA_HyperlinkControlTypeId;
varProp.lVal = UIA_EditControlTypeId;
//varProp.lVal = UIA_TextControlTypeId;
IUIAutomationCondition *pConditionHyperlink;
hr = _pUIAutomation->CreatePropertyCondition(UIA_ControlTypePropertyId, varProp, &pConditionHyperlink);
if (SUCCEEDED(hr))
{
hr = _pUIAutomation->CreateAndCondition(pConditionControlView, pConditionHyperlink, &pCondition);
pConditionHyperlink->Release();
pConditionHyperlink = NULL;
}
pConditionControlView->Release();
pConditionControlView = NULL;
IUIAutomationElementArray *_pElementArray = NULL;
hr = pElementBrowser->FindAll(TreeScope_Subtree, pCondition, &_pElementArray);
int cElements = 0;
hr = _pElementArray->get_Length(&cElements);
if (SUCCEEDED(hr) && (cElements > 0))
{
// Process each returned hyperlink element.
for (int idxElement = 0; idxElement < cElements; idxElement++)
{
IUIAutomationElement *pElement = NULL;
hr = _pElementArray->GetElement(idxElement, &pElement);
if (FAILED(hr))
{
break;
}
// Get the name property for the hyperlink element. How we get this depends
// on whether we requested that the property should be cached or not.
BSTR bstrName = NULL;
hr = pElement->get_CurrentName(&bstrName);
if (NULL == bstrName)
{
IUIAutomationValuePattern *pUIAValuePattern;
hr = pElement->GetCurrentPatternAs(UIA_ValuePatternId, IID_PPV_ARGS(&pUIAValuePattern));
if (SUCCEEDED(hr) && (pUIAValuePattern != NULL))
{
// Now get the Value property.
hr = pUIAValuePattern->get_CurrentValue(&bstrName);
pUIAValuePattern->Release();
pUIAValuePattern = NULL;
}
}
m_list.AddString(bstrName);
SysFreeString(bstrName);
}
}
if (pCondition != NULL)
{
pCondition->Release();
pCondition = NULL;
}
}