Delphi GetElementByClass?
如何从类名为的元素中获取InnerTextDelphi GetElementByClass?,delphi,delphi-xe2,twebbrowser,Delphi,Delphi Xe2,Twebbrowser,如何从类名为的元素中获取InnerText <div class="SomeClass" style="text-align: left; display: block;"></div> <div class="SomeClass" style="text-align: left; display: block;">Sometext</div> 一些文字 对于HTML文档中的查找值,hi必须具有相同id的特殊属性 它是重要的特殊属性 例如,您
<div class="SomeClass" style="text-align: left; display: block;"></div>
<div class="SomeClass" style="text-align: left; display: block;">Sometext</div>
一些文字
对于HTML文档中的查找值,hi必须具有相同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;
函数GetInnerElementById(常量文档:IDispatch;常量Id:string):宽字符串;
变量
文件:IHTMLDocument2;//文档的IHTMLDocument2接口
正文:IHTMLElement2;//文档体元素
标记:IHTMLElementCollection;//文档体中的所有标记
标签:IHTMLElement;//文档体中的标记
I:整数;//循环通过文档体中的标记
开始
结果:='';
//检查有效文档:需要IHTMLDocument2接口
如果不支持(文档、IHTMLDocument2、文档),则
引发异常。创建('无效HTML文档');
//检查有效的body元素:需要IHTMLElement2接口
如果不支持(Document.body、IHTMLElement2、body),则
引发异常。创建('找不到元素');
//获取body元素中的所有标记('*'=>任何标记名)
标记:=Body.getElementsByTagName('*');
//扫描正文中的所有标签
对于I:=0到Pred(Tags.length)do
开始
//获取对标记的引用
标记:=标记。项目(I,EmptyParam)作为IHTMlement;
//检查标签的id,如果id匹配则返回
如果是AnsiSameText(Tag.id,id),则
开始
结果:=Tag.innerHTML;
打破
结束;
结束;
结束;
您必须使用“MSHTML”单元
您可以将其与示例一起使用:
</head>
<body>
<div id="TESTID">sametext</div>
</body>
ShowMessage(GetElementById(wb1.Document,'TESTID'));
sametext
ShowMessage(GetElementById(wb1.Document,'TESTID');
如果您必须使用某个类,请告诉我我为您提供了新函数……好的,该类可能不止一个,您必须使用我为您制作的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;
不要忘记将MSHTML单元添加到主单元。是,请使用类名:)
var
lst : TStringList;
begin
//
lst := TStringList.Create;
GetInnersByClass(wb1.Document,'SameClass',lst);
ShowMessage(lst.Text);
lst.Free;
end;