用下面的代码可以获取iframe里的代码,现在的问题如果iframe是嵌套的,怎么获取源代码呢,困扰很久了,望高人指点。usr mshtml;
procedure TMainForm.ToolButton56Click(Sender: TObject);
var
  Index:      Integer;
  Document:   IHTMLDocument2;
  FrameIdx:   OleVariant;
  FrameDis:   IDispatch;
  FrameWin:   IHTMLWindow2;
begin
  while Webbrowser1.ReadyState <> READYSTATE_COMPLETE do
    Application.ProcessMessages;  if WebBrowser1.Document = nil then Exit;
  if WebBrowser1.Document.QueryInterface(IHTMLDocument2, Document) <> 0 then Exit;
  if Document.frames.length > 0 then
  begin
    for Index := 0 to Document.frames.length - 1 do
    begin
      FrameIdx := Index;
      FrameDis := Document.frames.item(FrameIdx);
      if FrameDis.QueryInterface(IHTMLWindow2, FrameWin) <> 0 then Exit;
      ShowMessage(FrameWin.document.body.outerHTML);  
      //FrameWin.document 就是你要的每个 Frame 的文档
    end;
  end;
end;

解决方案 »

  1.   

    提示一句,一个嵌套的框架,就是一个IWebBrowser...
      

  2.   

    //参考如下代码:
    procedure TForm1.Button1Click(Sender: TObject);
      procedure pScanFrames(mHTMLDocument2: IHTMLDocument2);
      var
        vIndex: OleVariant;
        I: Integer;
        vDispatch: IDispatch;
      begin
        if not Assigned(mHTMLDocument2) then Exit;
        Memo1.Lines.Add(mHTMLDocument2.body.outerHTML);
        for I := 0 to mHTMLDocument2.frames.length - 1 do
        begin
          vIndex := I;
          vDispatch := mHTMLDocument2.frames.item(vIndex);
          pScanFrames((vDispatch as IHTMLWindow2).document);
        end;
      end;
    begin
      pScanFrames(WebBrowser1.Document as IHTMLDocument2);
    end;
      

  3.   

    http://www.euromind.com/iedelphi/embeddedwb/framefunctions.htm
      

  4.   

    我画了一个图,你应该看得懂,看懂了就知道你问题的答案了。
    http://halfyawn.mblogger.cn
      

  5.   

    procedure TFormMain.NavigateFrameset(document: IHTMLDocument2);
    var
      index: Integer;
      ole_index: OleVariant;
      frame_dispatch: IDispatch;
      framed: IHTMLWindow2;
    begin
      if document = nil then
        exit;
      try
        if document.body <> nil then
          HtmlCode.Add(string(document.body.innerHTML) + document.URL);
        for index := 1 to document.Frames.Length do
        try
          ole_index := index - 1;
          frame_dispatch := document.Frames.Item(ole_index);
          if frame_dispatch <> nil then
          begin
            framed := frame_dispatch as IHTMLWindow2;
            NavigateFrameset(framed.document);
          end;
        except
          on E: Exception do
          begin
          end
        end;
      except
        on E: Exception do
        begin
          Application.MessageBox(PChar(E.Message),
            PChar('Exception'));
        end;
      end;
    end;
      

  6.   

    感谢大家,特别是:zswang(伴水清清)(专家门诊清洁工), jiangsheng(蒋晟.MSMVP2004Jan), halfdream(哈欠) 和 jinjazz(近身剪(N-P攻略)) 。