title: Delphi: TWebBrowser - Get all href of anchor tags of a class
body: <p>I want to get href of all  tags of class "pagination pagination_wrapper".
  I am using Delphi XE2 and TWebBrowser</p>
  
  <pre><code>&lt;ul class="pagination pagination_wrapper"&gt; 
      &lt;li&gt;
          &lt;a href="http://domain.com/1"&gt;1&lt;/a&gt;
      &lt;/li&gt; 
      &lt;li&gt;
          &lt;a href="http://domain.com/2"&gt;2&lt;/a&gt;
      &lt;/li&gt; 
      &lt;li&gt;
          &lt;a href="http://domain.com/3"&gt;3&lt;/a&gt;
      &lt;/li&gt;
      &lt;li&gt;
          &lt;a href="http://domain.com/4"&gt;4&lt;/a&gt;
      &lt;/li&gt; 
      &lt;li&gt;
          &lt;a href="javascript:void(0);"&gt;#&lt;/a&gt;
      &lt;/li&gt; 
  &lt;/ul&gt;
  </code></pre>
  
  <p>I have tried a lot but no success.
  There may be TYPO please ignore.
  I need a working code.</p>
  
  <p>Here is a code snippet I have tried with no luck.</p>
  
  <pre><code>var
      MyDiv, HtmlElem1 : IHTMLElement;
      HtmlAnchor       : IHTMLAnchorElement;
      AnCol1, AnCol2   : IHTMLElementCollection;
  begin
  
    GetElementByClass(ewb.Document, 'pagination pagination_wrapper', MyDiv); // works fine
    Supports(MyDiv.children, IHTMLElementCollection, AnCol1);
  
    ShowIt(AnCol1.Length-1);      // 5
  
    for i := 0 to AnCol1.Length-1 do
    begin
      if Supports(AnCol1.item(i,0), IHTMLElement, HtmlElem1) then
      begin
        ShowIt('HtmlElem1: '+HtmlElem1.innerHTML);    // shows perfect &lt;a&gt; HTML markup
  
        Supports(HtmlElem1.children, IHTMLElementCollection, AnCol2);
        ShowIt(AnCol2.length);    // 1
  
        Supports(AnCol2.tags('a'), IHTMLElementCollection, AnCol2);
  
        ShowIt(AnCol2.length);    // 1
  
        if Supports(AnCol2.item(i, 0), IHTMLAnchorElement, HtmlAnchor) then
          ShowIt(HtmlAnchor.href);
        else
          ShowIt('Not Anchor element');   // always prints this, even IHTMLElement does not work
      end
    end;
  end;
  </code></pre>
  
  <p>=========================================================================</p>
  
  <pre><code>function GetElementByClass(const Doc: IDispatch; const ClassName: string; var element : IHTMLElement): Boolean;
  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 := False ;
    element := nil;
  
    if not Supports(Doc, IHTMLDocument2, Document) then
      raise Exception.Create('Invalid HTML document');
  
    if not Supports(Document.body, IHTMLElement2, Body) then
      raise Exception.Create('Can''t find &lt;body&gt; element GetElementByClass()');
  
    Tags := Body.getElementsByTagName('*');
  
    for i := 0 to Pred(Tags.length) do
    begin
      Tag := Tags.item(I, EmptyParam) as IHTMLElement;
      if Pos(ClassName, string(Tag.className)) &gt; 0 then
      begin
        element := Tag;
        Result := True;
        Break;
      end;
    end;
  end;
  </code></pre>
  
  <p>Thanks in advance.</p>