Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I get HTML source code from TWebBrowser

How can I get source code from WebBrowser component?

I want to get source code of active page on WebBrowser component and write it to a Memo component.

Thanks.

like image 933
Someone Avatar asked Apr 10 '12 15:04

Someone


4 Answers

Why not Quick and Dirty:

OnNavigateComplete2()

Form1.RichEdit1.Text:=(WebBrowser1.OleObject.Document.documentElement.outerhtml);
like image 199
MasterDiesel Avatar answered Nov 09 '22 14:11

MasterDiesel


You can use the IPersistStreamInit Interface and the save method to store the content of the Webbrowser in a Stream.

Uses 
  ActiveX;

function GetWebBrowserHTML(const WebBrowser: TWebBrowser): String;
var
  LStream: TStringStream;
  Stream : IStream;
  LPersistStreamInit : IPersistStreamInit;
begin
  if not Assigned(WebBrowser.Document) then exit;
  LStream := TStringStream.Create('');
  try
    LPersistStreamInit := WebBrowser.Document as IPersistStreamInit;
    Stream := TStreamAdapter.Create(LStream,soReference);
    LPersistStreamInit.Save(Stream,true);
    result := LStream.DataString;
  finally
    LStream.Free();
  end;
end;
like image 38
RRUZ Avatar answered Nov 09 '22 15:11

RRUZ


That works well too:

    uses MSHTML;

    function GetHTML(w: TWebBrowser): String;
    Var
      e: IHTMLElement;
    begin
      Result := '';
      if Assigned(w.Document) then
      begin
         e := (w.Document as IHTMLDocument2).body;
    
         while e.parentElement <> nil do
         begin
           e := e.parentElement;
         end;
    
         Result := e.outerHTML;
      end;
    end;
like image 25
Mehmet Fide Avatar answered Nov 09 '22 15:11

Mehmet Fide


This has been asked and answered many times in the Embarcadero forums, with plenty of code examples posted. Search the archives.

The gist of it is that you Navigate() to the desired URL and wait for the OnDocumentComplete event to fire, then QueryInterface() the Document property for the IPersistStreamInit interface and call its save() method. Create a TStream object instance, such as a TMemoryStream, wrap it in a TStreamAdapter object, and then pass the adapter to save(). You can then load the TStream into the TMemo as needed.

like image 35
Remy Lebeau Avatar answered Nov 09 '22 15:11

Remy Lebeau