0
このコード(別の記事から取られています)は、古いMS Edgeバージョンでうまくいきました。 edgeElement = nilを取得します。これをどのように修正するべきですか?Delphi:Edge ver 38+(すべてのWindows 10アップデートがインストールされています)
procedure TForm1.Button1Click(Sender: TObject);
const
propertyName : integer = 30005; // UIA_NamePropertyId
propertyAutomationId : integer = 30011; // UIA_AutomationIdPropertyId
propertyClassName : integer = 30012; // UIA_ClassNamePropertyId
propertyNativeWindowHandle : integer = 30020; // UIA_NativeWindowHandlePropertyId
edgeAutomationId : WideString = 'TitleBar';
urlElementClassName : WideString = 'Internet Explorer_Server';
tabsListAutomationId : WideString = 'TabsList';
selectionPatternId : integer = 10001; // UIA_SelectionPatternId
var
uiAutomation : IUIAutomation;
rootElement,edgeElement,urlElement,tabsListElement,titleElement : IUIAutomationElement;
hresult : longint;
condition,conditionUrl,conditionTabsList : IUIAutomationCondition;
cacheRequestNativeWindowHandle,cacheRequest : IUIAutomationCacheRequest;
edgeWindowHandle : Pointer;
url : WideString;
selectionPattern : IUnknown;
elementArray : IUIAutomationElementArray;
title : WideString;
a,b: cardinal;
begin
//rootElement:=nil;
a := gettickcount;
try
uiAutomation:= CoCUIAutomation.Create;
except
showmessage('not supported');
exit;
end;
hresult:= uiAutomation.GetRootElement(rootElement);
if hresult<>S_OK then begin showmessage('Error GetRootElement');exit;end;
// Get the main Edge element, which is a direct child of the UIA root element.
// For this test, assume that the Edge element is the only element with an
// AutomationId of "TitleBar".
hresult:= uiAutomation.CreatePropertyCondition(propertyAutomationId, edgeAutomationId, condition);
if hresult<>S_OK then begin showmessage('Error CreatePropertyCondition');exit;end;
// Have the window handle cached when we find the main Edge element.
hresult:= uiAutomation.CreateCacheRequest(cacheRequestNativeWindowHandle);
if hresult<>S_OK then begin showmessage('Error CreateCacheRequest');exit;end;
hresult:=cacheRequestNativeWindowHandle.AddProperty(propertyNativeWindowHandle);
if hresult<>S_OK then begin showmessage('Error cacheRequestNativeWindowHandle.AddProperty');exit;end;
hresult:= rootElement.FindFirstBuildCache(TreeScope_Children, condition, cacheRequestNativeWindowHandle, edgeElement);
if hresult<>S_OK then begin showmessage('Error rootElement.FindFirstBuildCache');exit;end;
b := gettickcount;
if edgeElement<>nil then
begin
hresult:= edgeElement.Get_CachedNativeWindowHandle(edgeWindowHandle);
if hresult<>S_OK then begin showmessage('Error edgeElement.CachedNativeWindowHandle');exit;end;
// Next find the element whose name is the url of the loaded page. And have
// the name of the element related to the url cached when we find the element.
hresult:= uiAutomation.CreateCacheRequest(cacheRequest);
if hresult<>S_OK then begin showmessage('Error uiAutomation.CreateCacheRequest');exit;end;
hresult:=cacheRequest.AddProperty(propertyName);
if hresult<>S_OK then begin showmessage('Error cacheRequest.AddProperty');exit;end;
// For this test, assume that the element with the url is the first descendant element
// with a ClassName of "Internet Explorer_Server".
hresult:= uiAutomation.CreatePropertyCondition(propertyClassName, urlElementClassName, conditionUrl);
if hresult<>S_OK then begin showmessage('Error CreatePropertyCondition');exit;end;
hresult:= edgeElement.FindFirstBuildCache(TreeScope_Descendants, conditionUrl, cacheRequest, urlElement);
if hresult<>S_OK then begin showmessage('Error edgeElement.FindFirstBuildCache');exit;end;
hresult:= urlElement.Get_CachedName(url);
if hresult<>S_OK then begin showmessage('Error urlElement.Get_CachedName');exit;end;
// Next find the title of the loaded page. First find the list of
// tabs shown at the top of Edge.
hresult:= uiAutomation.CreatePropertyCondition(propertyAutomationId, tabsListAutomationId, conditionTabsList);
if hresult<>S_OK then begin showmessage('Error uiAutomation.CreatePropertyCondition');exit;end;
hresult:= edgeElement.FindFirst(TreeScope_Descendants, conditionTabsList, tabsListElement);
if hresult<>S_OK then begin showmessage('Error edgeElement.FindFirst');exit;end;
// Find which of those tabs is selected. (It should be possible to
// cache the Selection pattern with the above call, and that would
// avoid one cross-process call here.)
hresult:= tabsListElement.GetCurrentPattern(selectionPatternId, selectionPattern);
if hresult<>S_OK then begin showmessage('Error tabsListElement.GetCurrentPattern');exit;end;
// For this test, assume there's always one selected item in the list.
hresult:= (selectionPattern as IUIAutomationSelectionPattern).GetCurrentSelection(elementArray);
if hresult<>S_OK then begin showmessage('Error selectionPattern.GetCurrentSelection');exit;end;
hresult:= elementArray.GetElement(0, titleElement);
if hresult<>S_OK then begin showmessage('Error elementArray.GetElement');exit;end;
hresult:= titleElement.Get_CurrentName(title);
if hresult<>S_OK then begin showmessage('Error titleElement.Get_CurrentName');exit;end;
b := gettickcount;
showmessage('Page title: ' + title+#13#10+
'URL: '+url+#13#10+
'hwnd: '+IntToStr(Integer(edgeWindowHandle)) +#13#10+
'time: ' + inttostr(b-a));
end
else showmessage('edgeElement = nil' + ', time: ' + inttostr(b-a));
b := gettickcount;
caption := inttostr(b-a)
end;
に更新..あなたがあなたのコードを修正してくださいだろうか? –