How can I get InnerText from Element with Class Name?
<div class="SomeClass" style="text-align: left; display: block;"></div>
<div class="SomeClass" style="text-align: left; display: block;">Sometext</div>
How can I get InnerText from Element with Class Name?
<div class="SomeClass" style="text-align: left; display: block;"></div>
<div class="SomeClass" style="text-align: left; display: block;">Sometext</div>
hi for find value in HTML doc yo must have special property same id .
it is important special property .
for example you can find inner text with this function but with 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;
you must use the "MSHTML" unit...
and you can use it with sample:
</head>
<body>
<div id="TESTID">sametext</div>
</body>
ShowMessage(GetElementById(wb1.Document,'TESTID'));
if you have to use SomeClass tell me i give new function to you....
ok the class maybe more than one you must use a TstringList i made function for you:
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;
result is how many class each with function
and you cam use it with this sample :
var
lst : TStringList;
begin
//
lst := TStringList.Create;
GetInnersByClass(wb1.Document,'SameClass',lst);
ShowMessage(lst.Text);
lst.Free;
end;
dont forget add MSHTML unit to main unit.