我试图制作一个小工具,从访问过的网站下载所有图像。它必须使用twebbrowser组件。我客户的测试网站是Click。目前我用getelementbyid选择图片,但有些图片没有id。我如何解决失踪者?非常感谢
答案 0 :(得分:3)
加载页面后,查询IHTMLDocument2
接口的TWebBrowser.Document
属性,然后您可以枚举IHTMLDocument2.images
集合的元素:
var
Document: IHTMLDocument2;
Images: IHTMLElementCollection;
Image: IHTMLImgElement;
I: Integer;
begin
Document := WebBrowser1.Document as IHTMLDocument2;
Images := Document.images;
For I := 0 to Images.length - 1 do
begin
Image := Images.item(I, '') as IHTMLImgElement;
// use Image as needed...
end;
end;
请注意,这只会在HTML <img>
标记中找到图片。如果您还需要在<input type="image">
标记中查找图像,则必须枚举IHTMLDocument2.all
集合的元素,以查找IHTMLInputElement
属性为type
的接口的实例"image"
,例如:
var
Document: IHTMLDocument2;
Elements: IHTMLElementCollection;
Element: IHTMLElement;
Image: IHTMLImgElement;
Input: IHTMLInputElement;
I: Integer;
begin
Document := WebBrowser1.Document as IHTMLDocument2;
Elements := Document.all;
For I := 0 to Elements.length - 1 do
begin
Element := Elements.item(I, '') as IHTMLElement;
if Element is IHTMLImgElement then begin
Image := Element as IHTMLImgElement;
// use Image as needed...
end
else if Element is IHTMLInputElement then begin
Input := Element as IHTMLInputElement;
if Input.type = 'image' then
begin
// use Input as needed...
end;
end;
end;
end;
答案 1 :(得分:0)
您可以使用WebDocument.all.item(itemnum,'')来“遍历”文档并查看每个元素,而不是通过id请求特定元素。
var
cAllElements: IHTMLElementCollection;
eThisElement: IHTMLElement;
WebDocument: IHTMLDocument2;
=======
cAllElements:=WebDocument.All
For iThisElement:=0 to cAllElements.num-1 do
begin
eThisElement:=cAllElements.item(iThisElement,'') as IHTMLElement;
// check out eThisElement and do what you want
end;
然后,您将查看IMG的元素.tagName,或者执行您需要的任何评估,以确定它是否是图片并像以前一样处理它。
丹