遍历页面元素

代码
uses
  shdocvw, Mshtml, ActiveX;

function GetIEFromHWND(WHandle: HWND; var IE: IWebbrowser2): HRESULT;
var
  hInst: HWND;
  lRes: Cardinal;
  MSG: Integer;
  pDoc: IHTMLDocument2;
  ObjectFromLresult: TObjectFromLresult;
begin
   hInst :
= LoadLibrary('Oleacc.dll');
   @ObjectFromLresult :
= GetProcAddress(hInst, 'ObjectFromLresult');
   
if @ObjectFromLresult <> nil then
   
begin
     
try
       MSG :
= RegisterWindowMessage('WM_HTML_GETOBJECT');
       SendMessageTimeOut(WHandle, MSG, 
00, SMTO_ABORTIFHUNG, 1000, lRes);
       Result :
= ObjectFromLresult(lRes, IHTMLDocument2, 0, pDoc);
       
if Result = S_OK then
         (pDoc.parentWindow 
as IServiceprovider).QueryService(IWebbrowserApp, IWebbrowser2, IE);
     
finally
       FreeLibrary(hInst);
     
end;
   
end;
end;

procedure TForm1.Button2Click(Sender: TObject);
var
  I: IWebbrowser2;
  Document: IHtmlDocument2;
  HtmlElement: IHtmlElement;
  All: IHtmlElementCollection;
  J: Integer;
begin
  GetIEFromHWND(
66000, I); // 66000是我自己测试用的句柄
  Document :
= I.Document as IHtmlDocument2;
  
if Assigned(Document) then
  
begin
    All :
= Document.all;
    
for J := 0 to All.length - 1 do
    
begin
      HtmlElement :
= All.item(J, 0as IhtmlElement;
      Memo1.Lines.Add(IntToStr(J) 
+ ' ' + HTmlElement.innerHTML);
    
end;
  
end;  
  Caption :
= i.LocationURL;
end;
原文地址:https://www.cnblogs.com/chengxin1982/p/1762033.html