• 遍历页面元素


    代码
    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;
  • 相关阅读:
    LeetCode 1356. 根据数字二进制下1的数目排序
    Ubuntu LaTeX 中文环境配置 与 VSCode LaTeX Workshop
    LeetCode 57. 插入区间
    VSCode Ubuntu下调试失败 无法打开 libc-start.c raise.c等
    LeetCode 30. 串联所有单词的子串
    日期处理函数
    Stream 和 byte[] 之间的转换
    Math.Round {实现四舍五入的小技巧}
    重写alert弹窗
    js轮播图
  • 原文地址:https://www.cnblogs.com/chengxin1982/p/1762033.html
Copyright © 2020-2023  润新知