delphi - 访问由 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,我想这可能是第二张图像的显示方式,但我不知道如何获取它。
有任何想法吗?
解决方案
您链接的网站中的第二张图片位于 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.
保存实际图像已包含在另一个问题中
推荐阅读
- php - laravel DB查询选择多个表而不加入
- ios - Swift:在后台运行时计时器延迟触发
- html - 不确定如何为背景图像中的图像设置正确的路径
- admin-on-rest - 在 /admin 下挂载 react-admin
- python - Python 返回值只打印对象中的一个值,而不是所有值
- java - 如何在没有 Internet 连接的情况下使用 Maven 安装 JDBC 驱动程序
- swift - 闭包的弱变量之类的东西
- javascript - 状态更改后立即在 React 中获取最新的 Redux 状态
- reactjs - 创建反应应用程序 - ES6 和 passport.js
- elasticsearch - ElasticSearch 6 中的字段名称规则?