How to get the source code of a page through WebBrowser-Control (ActiveX InternetExplorer)?

How can I get the source code of a page through WebBrowser Control (ActiveX InternetExplorer)?

I have an xml document "foo.xml".

var Web: TWebBrowser; begin ... Web.Navigate("foo.xml"); // How can I get source code thru WebBrower control<---- ... end; 
+6
delphi webbrowser-control
source share
7 answers

I thought it would be easy, but it looks like it could be forgotten. You can easily do this with TidHTTP management.

 MyPage := IdHTTP1.Get('www.google.com'); 

I know that this is not what you want, but it can help.

+1
source share

In the DocumentCompleted event, view the DocumentText property of the WebBrowser control. It should have the full text of the loaded page.

+1
source share
 IHTMLDocument2(Web.Document).Body.InnerHTML; 

This should return the source of the page.

+1
source share

Another method that works well is using Synapse . Use the HttpGet syntax call to retrieve the source resource (which gives the source code), and then manipulate as needed.

Another option is to use the EmbeddedWB component, which provides MANY additional properties and functions of the web browser than the standard Delphi component, and is still suitable for your requirement to do this in a web browser.

+1
source share

To access the entire HTML page of a page using the WebBrowser control, follow these steps:

 Web.Document.All[0].OutterHtml; 
+1
source share
 private void btnTest_Click(object sender, EventArgs e) { wbMain.Navigate("foo.xml"); wbMain.DocumentCompleted += new WebBrowserDocumentCompletedEventHandler(testing); } private void testing(Object sender, WebBrowserDocumentCompletedEventArgs e) { test = wbMain.DocumentText; } 

I know this is a little late, but it works for me. wbMain is a WebBrowser object.

+1
source share

WebBrowser1.Navigate () loads it into the RAD component window using the built-in IE component in Windows. What you do is answer the callback (for the browser component, double-click the OnDownloadComplete event) and save it in a file in this function. Fragments from the working code:

 procedure TMainForm.WB_SaveAs_HTML(WB : TWebBrowser; const FileName : string) ; var PersistStream: IPersistStreamInit; Stream: IStream; FileStream: TFileStream; begin if not Assigned(WB.Document) then begin Logg('Document not loaded!') ; //'Logg' adds a line to a log file. Exit; end; PersistStream := WB.Document as IPersistStreamInit; FileStream := TFileStream.Create(FileName, fmCreate) ; try Stream := TStreamAdapter.Create(FileStream, soReference) as IStream; if Failed(PersistStream.Save(Stream, True)) then ShowMessage('SaveAs HTML fail!') ; finally FileStream.Free; end; end; (* WB_SaveAs_HTML *) procedure TMainForm.WebBrowser1DownloadComplete(Sender: TObject); begin if (WebBrowser1.Document<>nil)AND NOT(WebBrowser1.busy) then begin WB_SaveAs_HTML(WebBrowser1,'test.html'); //myStringList.loadFromFile('test.html'); //process it. end; end; 

Note that some MIME types ("file"), such as JSON, open the "Save As ..." dialog box in IE, which stops reading and requires manual intervention.

+1
source share

All Articles