首页 > 解决方案 > 访问由 TWebBrowser 显示但未包含在其图像集合中的图像

问题描述

这里的网页出现在另一个关于从电子表格中检索图像的问题中。

如果您导航到 FF 中的页面,您会看到有两个图像,位于蓝色标题条的 LHS。

但是,如果我将页面加载到 TWebBrowser 并运行以下代码

procedure TForm1.GetImageCount;
var
  Count : Integer;
  Doc : IHtmlDocument2;
begin
  Doc := IDispatch(WebBrowser1.Document) as IHtmlDocument2;
  Count := Doc.images.length;
  ShowMessageFmt('ImageCount: %d', [Count]);
end;

,消息框报告的计数为 1 而不是预期的(无论如何由我) 2. 我可以轻松访问并保存到磁盘显示的第一个图像,但不是第二个或任何后续图像,因为它们不在已加载页面的IHtmlDocument2Images集合。

所以我的问题是,如何获取第二张图像以将其保存到磁盘?

FF 调试器显示网页中塞满了 javascript,我想这可能是第二张图像的显示方式,但我不知道如何获取它。

有任何想法吗?

标签: delphitwebbrowser

解决方案


您链接的网站中的第二张图片位于 iframe 中。您可以从OnDocumentComplete事件中访问 iframe:

unit Unit1;

interface

uses
  Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants, System.Classes, Vcl.Graphics,
  Vcl.Controls, Vcl.Forms, Vcl.Dialogs, Vcl.OleCtrls, SHDocVw, MsHtml;

type
  TForm1 = class(TForm)
    WebBrowser1: TWebBrowser;
    procedure WebBrowser1DocumentComplete(ASender: TObject;
      const pDisp: IDispatch; const URL: OleVariant);
    procedure FormShow(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
  end;

var
  Form1: TForm1;

implementation

{$R *.dfm}



procedure TForm1.FormShow(Sender: TObject);
begin
 WebBrowser1.Navigate('https://www.nbbclubsites.nl/club/8000/uitslagen');
end;

procedure TForm1.WebBrowser1DocumentComplete(ASender: TObject; const pDisp: 

IDispatch; const URL: OleVariant);

var
  currentBrowser: IWebBrowser;
  topBrowser: IWebBrowser;
  Doc : IHtmlDocument2;

begin
  currentBrowser := pDisp as IWebBrowser;
  topBrowser := (ASender as TWebBrowser).DefaultInterface;
  if currentBrowser = topBrowser then
   begin
    // master document
    Doc := currentBrowser.Document as IhtmlDocument2;
    ShowMessageFmt('ImageCount: %d', [Doc.images.length]);
   end
  else
  begin
   // iframe
   Doc := currentBrowser.Document as IhtmlDocument2;
   ShowMessageFmt('ImageCount: %d', [Doc.images.length]);
  end;
end;

end.

保存实际图像已包含在另一个问题中


推荐阅读