GetElementByClass?

时间:2012-11-18 19:25:25

标签: delphi delphi-xe2 twebbrowser

如何从具有类名的Element获取InnerText?

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

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

2 个答案:

答案 0 :(得分:1)

嗨,对于HTML doc中的find值,你必须具有相同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告诉我,我给你新的功能....

答案 1 :(得分: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单元添加到主单元。