从Delphi的现有Internet Explorer实例获取HTML

时间:2013-10-12 12:17:26

标签: html delphi internet-explorer mshtml twebbrowser

在特定的网站上,如果我打开IE的新实例,我已经通过身份验证,只要同时是我之前登录过的IE的另一个打开实例。 如果我从Delphi打开一个tWebbrowser,我没有通过身份验证。 因为我不想在我创建的每个tWebbrowser中登录,所以我想找到一种方法,当我手动打开IE的新实例时,如何保持这种认证工作正常。 目的是从我登录的不同页面获取HTML。

我想要

1)从使用ShellExecute打开的Internet Explorer实例中获取HTML

2)让tWebbrowser记住来自IE的所有其他开放实例的身份验证

3)将tWebbrowser连接到现有的Internet Explorer实例

4)从Delphi获取HTML的其他方法

身份验证我无法找到如何自动化,因为它是java / sso。

1 个答案:

答案 0 :(得分:1)

您可以通过执行以下操作来查询Windows shell的IE窗口:

uses ShDocVw_Tlb; // or ShDocVw

if Doc = Nil then
  exit;
if Doc.body = Nil then
  exit;

var
  i: Integer;
  Browser: IWebBrowser2;
  ShellWindows: IShellWindows;
  Doc : IHtmlDocument2;

ShellWindows := CoShellWindows.Create;
for i := 0 to ShellWindows.Count - 1 do
  if Supports(ShellWindows.Item(i), IWebBrowser2, Browser) then 
  begin
    // do something with Browser instance, e.g compare the Url you're
    // expecting with Browser.LocationUrl
    // if it is, then you can get at the Html by something like

    Browser.Document.QueryInterface(IHtmlDocument2, Doc);
    if (Doc <> Nil) and (Doc.Body <> Nil) then  
    // access any of the Doc's properties, e.g. InnerHtml

  end;

显然,当Support返回true时,您可以检查返回的浏览器以查找您要查找的内容。例如。你可以使用浏览器文档的IHtmlDocument2界面(检查它不是第一个)来访问它的DOM,HTML等。我想相对简单地找到一个TWebBrowser窗口,如果这是什么你想做什么。