我正在使用上面的代碼來瀏覽一個視窗的后代,但是,它并沒有像我使用inspect.exe。
目前在 chrome 瀏覽器中進行測驗,在我寫這個問題的當前網頁中,它只捕捉到了當前打開的頁面、書簽和瀏覽器按鈕,卻沒有捕捉到頁面內容。
在其他視窗(例如檔案夾)中進行測驗時,它確實能夠正確地捕獲所有內容,但是它花費了太多的時間,在一個有120個元素的檔案夾中,它花費了1.4秒,我想知道這段代碼是否能夠得到任何改進?
#include <UIAutomation.h>/span>
#include <UIAutomationCore.h>/span>
#include <UIAutomationClient.h>/span>
#include "Oleacc.h"
#include "atlbase.h"
#pragma comment(lib,"Oleacc.lib")
WCHAR window[250] = L "Ask a Question - Stack Overflow - Google Chrome" ;
IUIAutomationElement *element = GetTopLevelWindowByName(window)。
ListDescendants(element, 2) 。
IUIAutomationElement* GetTopLevelWindowByName(LPWSTR windowName)
{
if (windowName == NULL)
return NULL。
CComPtr<IUIAutomation> g_pAutomation。
if (SUCCEED(CoInitialize(NULL)>)
{
if (!SUCCEED(g_pAutomation.CoCreateInstance(CLSID_CUIAutomation8))) //或CLSID_CUIAutomation。
return NULL。
}
VARIANT varProp;
varProp.vt = VT_BSTR;
varProp.bstrVal = SysAllocString(windowName)。
if (varProp.bstrVal == NULL)
return NULL;
IUIAutomationElement* pRoot = NULL;
IUIAutomationElement* pFound = NULL;
IUIAutomationCondition* pCondition = NULL;
//獲得桌面元素。
HRESULT hr = g_pAutomation->GetRootElement(&pRoot)。
if (FAILED(hr) || pRoot == NULL)
goto清理。
///通過名稱獲得一個頂層元素,如 "程式經理"。
hr = g_pAutomation->CreatePropertyCondition(UIA_NamePropertyId, varProp, &pCondition)。
if (FAILED(hr))
goto cleanup;
pRoot->FindFirst(TreeScope_Children, pCondition, &pFound) 。
清理。
if (pRoot != NULL)
pRoot->Release()。
if (pCondition != NULL)
pCondition->Release()。
VariantClear(&varProp)。
return pFound;
}
void ListDescendants(IUIAutomationElement* pParent, int indent)
{
static CComPtr<IUIAutomation> g_pAutomation;
if (!g_pAutomation) {
if (SUCCEED(CoInitialize(NULL) ))
{
if (!SUCCEED(g_pAutomation.CoCreateInstance(CLSID_CUIAutomation8))) //或CLSID_CUIAutomation。
return;
}
}
if (pParent == NULL)
return;
IUIAutomationTreeWalker* pControlWalker = NULL;
IUIAutomationElement* pNode = NULL;
g_pAutomation->get_ControlViewWalker(&pControlWalker)。
if (pControlWalker == NULL)
goto清理。
pControlWalker->GetFirstChildElement(pParent, &pNode)。
if (pNode == NULL)
goto清理。
while (pNode)
{
BSTR sName;
pNode->get_CurrentName(&sName)。
UIA_HWND uia_hwnd;
pNode->get_CurrentNativeWindowHandle(&uia_hwnd)。
RECT矩形。
pNode->get_CurrentBoundingRectangle(&rect)。
ListDescendants(pNode, indent 1) 。
IUIAutomationElement* pNext。
pControlWalker->GetNextSiblingElement(pNode, &pNext)。
pNode->Release()。
pNode = pNext。
}
清理。
if (pControlWalker != NULL)
pControlWalker->Release()。
if (pNode != NULL)
pNode->Release()。
return。
}
uj5u.com熱心網友回復:
使用Chrome 93版本和Windows 10進行測驗:
如果chrome視窗至少部分可見,chrome中的第一個UIA子檔案是活動標簽的html頁面,你可以在這個頁面中行走UIA元素。如果chrome視窗不可見,你不能用UIA訪問頁面內容。你只能使用UIA來訪問Chrome的主要控制元件,正如這里所解釋的
您可能在部分可見的 Chrome 視窗前以小視窗的形式運行 Inspect 工具,因此 Inspect 可以看到頁面內容。否則,檢查工具就沒有什么特別之處了。
Chrome 的另一個怪癖是,當 Chrome 視窗處于全屏模式時,UIA 無法看到主要控制元件。相反,UIA只看到頁面內容(只要Chrome沒有隱藏在另一個視窗后面)
這似乎是UIA的一個特點。
這似乎是針對 Chrome 瀏覽器的。火狐瀏覽器的表現與預期一致。
轉載請註明出處,本文鏈接:https://www.uj5u.com/qiye/320286.html
標籤:
