【问题标题】:GetElementByClass?按类获取元素?
【发布时间】:2012-11-18 19:25:25
【问题描述】:

如何从具有类名的元素中获取 InnerText?

<div class="SomeClass" style="text-align: left; display: block;"></div>

<div class="SomeClass" style="text-align: left; display: block;">Sometext</div>

【问题讨论】:

    标签: delphi delphi-xe2 twebbrowser


    【解决方案1】:

    好吧,这门课可能不止一个,你必须使用我为你制作的 TstringList 函数:

    function GetInnersByClass(const Doc: IDispatch; const classname: string;var Lst:TStringList): Integer;
    var
      Document: IHTMLDocument2;     // IHTMLDocument2 interface of Doc
      Body: IHTMLElement2;          // document body element
      Tags: IHTMLElementCollection; // all tags in document body
      Tag: IHTMLElement;            // a tag in document body
      I: Integer;                   // loops thru tags in document body
    begin
      Lst.Clear;
      Result := 0 ;
      // Check for valid document: require IHTMLDocument2 interface to it
      if not Supports(Doc, IHTMLDocument2, Document) then
        raise Exception.Create('Invalid HTML document');
      // Check for valid body element: require IHTMLElement2 interface to it
      if not Supports(Document.body, IHTMLElement2, Body) then
        raise Exception.Create('Can''t find <body> element');
      // Get all tags in body element ('*' => any tag name)
      Tags := Body.getElementsByTagName('*');
      // Scan through all tags in body
      for I := 0 to Pred(Tags.length) do
      begin
        // Get reference to a tag
        Tag := Tags.item(I, EmptyParam) as IHTMLElement;
        // Check tag's id and return it if id matches
        if AnsiSameText(Tag.className, classname) then
        begin
          Lst.Add(Tag.innerHTML);
          Inc(Result);
        end;
      end;
    end;
    

    结果是每个类有多少个功能

    你可以在这个示例中使用它:

    var
      lst : TStringList;
    begin
      //
      lst := TStringList.Create;
      GetInnersByClass(wb1.Document,'SameClass',lst);
      ShowMessage(lst.Text);
      lst.Free;
    end;
    

    不要忘记将 MSHTML 单元添加到主单元。

    【讨论】:

      【解决方案2】:

      hi for find value in HTML doc yo must have special property same id .

      这是重要的特殊属性。

      例如,您可以使用此函数查找内部文本,但使用 id :

      function GetInnerElementById(const Doc: IDispatch; const Id: string): WideString;
      var
        Document: IHTMLDocument2;     // IHTMLDocument2 interface of Doc
        Body: IHTMLElement2;          // document body element
        Tags: IHTMLElementCollection; // all tags in document body
        Tag: IHTMLElement;            // a tag in document body
        I: Integer;                   // loops thru tags in document body
      begin
        Result :='';
        // Check for valid document: require IHTMLDocument2 interface to it
        if not Supports(Doc, IHTMLDocument2, Document) then
          raise Exception.Create('Invalid HTML document');
        // Check for valid body element: require IHTMLElement2 interface to it
        if not Supports(Document.body, IHTMLElement2, Body) then
          raise Exception.Create('Can''t find <body> element');
        // Get all tags in body element ('*' => any tag name)
        Tags := Body.getElementsByTagName('*');
        // Scan through all tags in body
        for I := 0 to Pred(Tags.length) do
        begin
          // Get reference to a tag
          Tag := Tags.item(I, EmptyParam) as IHTMLElement;
          // Check tag's id and return it if id matches
          if AnsiSameText(Tag.id, Id) then
          begin
            Result := Tag.innerHTML;
            Break;
          end;
        end;
      end;
      

      您必须使用“MSHTML”单元...

      您可以将其与示例一起使用:

      </head>
      <body>
      <div id="TESTID">sametext</div>
      </body>
      
      
      ShowMessage(GetElementById(wb1.Document,'TESTID'));
      

      如果你必须使用 SomeClass 告诉我我给你新功能....

      【讨论】:

      • 是的,必须使用类名:)
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-01-14
      • 2012-03-09
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多