MartynA
MartynA

Reputation: 30715

Accessing images displayed by TWebBrowser but not contained in its Images collection

The web page here cropped up in another question here about retrieving imags from a spreadsheet.

If you navigate to the page in FF you'll see that there are two images, at the LHS of the blue header strips.

However, if I load the page into a TWebBrowser and run the following code

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

, the message box reports a count of 1 rather than the expected (by me, anyway) 2. I can easily access and save to disk the first image displayed, but not the second or any subsequent one, because they aren't in the IHtmlDocument2 Images collection of the loaded page.

So my question is, how do I get at the second image to save it to disk?

The FF debugger shows that the web page is crammed with javascript, which I imagine may be how the second image gets to be displayed, but I've no idea how to go about getting it.

Any ideas?

Upvotes: 3

Views: 278

Answers (1)

whosrdaddy
whosrdaddy

Reputation: 11860

The second image in the site you linked is located in an iframe. You can access the iframe from the OnDocumentComplete event:

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.

Saving the actual image is already covered in another question

Upvotes: 2

Related Questions