关于 UIAutomation 的问题,谢谢帮忙!

yusong1987 2013-10-15 07:13:44

最近在研究怎么获取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;
}
}

...全文
795 5 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
5 条回复
切换为时间正序
请发表友善的回复…
发表回复
allenhiman 2014-03-21
  • 打赏
  • 举报
回复
其实。net的api最先获得的也是UIA_EditControlTypeId 只不过它发现是只读的 就设定为.Text了 你的代码应该也对状态进行判断 是只读和非只读 html的元素 本来就没什么很严格的类型 甚至可以用一个div当做一个文本 这些都很可能
allenhiman 2014-03-21
  • 打赏
  • 举报
回复
呵呵 你得到editid的是用的c++代码吧 得到textid的是用的c#代码
yusong1987 2014-03-20
  • 打赏
  • 举报
回复
来个人 拿分吧~~~
yusong1987 2014-03-20
  • 打赏
  • 举报
回复
这么久了,确实一个回复都没有,除了我自己~ 算了,关贴!
yusong1987 2013-10-17
  • 打赏
  • 举报
回复
一个回复都木有,呼叫版主帮忙啊.. 自己刚做了几次测试还是不行哦..

3,248

社区成员

发帖
与我相关
我的任务
社区描述
ATL,Active Template Library活动(动态)模板库,是一种微软程序库,支持利用C++语言编写ASP代码以及其它ActiveX程序。
社区管理员
  • ATL/ActiveX/COM社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧