How can I get source code of page thru 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;
How can I get source code of page thru 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;
In the DocumentCompleted event, look at the DocumentText
property of the WebBrowser control. It should have the complete text of the loaded page.
IHTMLDocument2(Web.Document).Body.InnerHTML;
This should return the source of the page.
I thought this would be easy but it seems it might have been forgotten. You can easily do it with a TidHTTP control though.
MyPage := IdHTTP1.Get('www.google.com');
I know its not what you want but might help.
Another method which works well is to use Synapse. Use the synapse call HttpGet to retrieve your initial resource (which gives you the source code) then manipulate as needed.
Another option would be to use the EmbeddedWB component which exposes MANY more properties and features of the web browser than the standard Delphi component does and still fits your requirement of doing it within the web browser.
To access the entire HTML of the page through your WebBrowser control use:
Web.Document.All[0].OutterHtml;
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 this works for me. wbMain is the WebBrowser Object.
WebBrowser1.Navigate() loads it into the RAD component window using the built in IE component in the Windows OS. What you do is respond to a callback (for the browser component, double-click the OnDownloadComplete event) and save it to file in that function. Snippets from 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 ("file") types such as JSON give a 'Save As...' dialog in IE, which stops your reading and requires manual intervention.