加载AJAX后,Delphi TWebBrowser获得HTML源 [英] Delphi TWebBrowser get HTML source after AJAX load

查看:209
本文介绍了加载AJAX后,Delphi TWebBrowser获得HTML源的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下函数,可在DocumentComplete事件后获取HTML文档的源代码。

I have the following function that gets the HTML document's source code after DocumentComplete event.

function TBrowser.GetWebBrowserHTML(const WebBrowser: TWebBrowser): string;
var
  LStream: TStringStream;
  Stream : IStream;
  LPersistStreamInit : IPersistStreamInit;
begin
  try
    if not Assigned(WebBrowser.Document) then exit;
    LStream := TStringStream.Create('', TEncoding.UTF8);
    try
      LPersistStreamInit := WebBrowser.Document as IPersistStreamInit;
      Stream := TStreamAdapter.Create(LStream,soReference);
      LPersistStreamInit.Save(Stream,true);
      result := LStream.DataString;
    finally
      LStream.Free();
    end;
  except
  end;
end;

问题:在之前执行AJAX调用之前已检索源代码这页纸。
页面完成加载(由WebBrowser确定),但是AJAX继续修改DOM,其他元素显示在页面上。
我需要的是Mozilla的查看生成的源代码,或者是用Firebug或Chrome Inspector或IE开发人员工具检查网页时出现的html源代码。

The problem: the source code is retrieved before AJAX calls are performed on the page. The page finishes loading (as WebBrowser determines), but AJAX continues to modify the DOM and additional elements appear on the page. What I need is the equivalent of Mozilla's "View Generated Source", or the html source that appear when inspecting the web page with Firebug or Chrome Inspector or IE Developer Tools.

似乎在C语言中有具有此功能的DocumentText属性,但在Delphi中找不到实现此功能的任何属性或方法。

Seems that in C there is DocumentText property that does this thing, but couldn't find any property or methods to achieve this in Delphi.

任何想法/提示/帮助

推荐答案

您可以使用 IHTMLDocument2 接口, TWebBrowser.Document实现的接口。该属性以IDispatch的形式公开,但是您可以将其强制转换为接口或(Ole)Variant,尽管那样您将不会从代码完成中受益。

You can use the IHTMLDocument2 interface, which is the interface that TWebBrowser.Document implements. The property is exposed as an IDispatch, but you can cast it to the interface, or to an (Ole)Variant, although you won't benefit from code completion then.

IHTMLDocument2接口支持DocumentElement属性,该属性指向文档的根元素。该元素(和其他元素一样)具有属性 outerHTML ,该属性为您提供该元素及其所有内容作为字符串:

The IHTMLDocument2 interface supports the DocumentElement property, which points to the root element of the document. That element (as any other) has the property outerHTML, which gives you the element and all its contents as a string:

var
  d: OleVariant;
begin
  d := WebBrowser1.Document;
  ShowMessage(d.documentElement.outerHTML);

据我所知,这是文档的实际状态,包括所做的任何更改

As far as I can see, this is the actual state of the document, including any changes that are made by Javascript.

它似乎不包含doctype,但是如果我通过Webbrowser1.Document.All找到了doctype元素,那么它的externalHTML属性不返回任何东西。文档的其他部分也进行了更改(大写的标签名称为一个),但这仅确认这是基于加载的DOM而非文档的原始来源生成的文档结构。

It doesn't seem to include the doctype, but then again, if I find the doctype element through Webbrowser1.Document.All, then its outerHTML property doesn't return anything. Other parts of the document are also changed (tag names in capitals, for one), but that only confirms that this is a generated document structure based on the loaded DOM, rather than the original source of the document.

这篇关于加载AJAX后,Delphi TWebBrowser获得HTML源的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆