I'm trying to get the text of every frame open in a browser window. I'm a
bit flummoxed because the IHTMLFramesCollection2 seems to return something
with no properties I can access.
How do I take the elements of the Frames Collection and access the text for
each of them? Am I going about this the right way, or is there another
method that would return the entire text for the webpage?
procedure TForm1.IE4VersionbtnClick(Sender: TObject);
var
i: Integer;
Browser: IWebBrowser2;
ShellWindows: IShellWindows;
WebPage:IHTMLDocument2;
Frames:IHTMLFramesCollection2;
Links:IHTMLElementCollection;
Link:IHTMLElement;
HtmlStr:string;
dummy,
FrameContent,j,flags:OLEVariant;
begin
{ Use ShellWindows to get the active browser window }
ShellWindows := CoShellWindows.Create;
for i := 0 to ShellWindows.Count - 1 do
begin
Browser := ShellWindows.Item(i) as IWebBrowser2;
try
WebPage := Browser.Document as IHtmlDocument2;
except
end;
if (Assigned(WebPage)) then
begin
Frames:=Webpage.frames as IHTMLFramesCollection2;
j:=0;
while integer(j) < frames.length do
begin
FrameContent:=frames.item(j); (*********** I don't have a
clue what to do with it now *****}
inc(j);
end;
end;
end;
end;
Thanks for any help.
Rik.
I'm not sure, but I think IHTMLDocument:= frames.item(j) as IHTMLDocument2
could work. I think, so you can get the document assigned to the frame.
Hans-Jürgen
You're nearly there:
innerframesCol : iHTMLFramesCollection2;
innerFramesLen : integer;
InnerFrameWin : IHTMLWindow2;
InnerpickFrame : olevariant;
innerframe : integer;
WebPage := Browser.Document as IHtmlDocument2
InnerFramesCol := WebPage.Get_frames;
InnerFramesLen := InnerFramesCol.Get_length;
for innerframe := 0 to InnerFramesLen - 1 do
begin
innerpickframe := innerframe;
docdisp := InnerFramesCol.item(Innerpickframe);
OleCheck(docdisp.QueryInterface(IID_IHTMLWindow2,
InnerFrameWin));
WebDoc := InnerFrameWin.Get_document;
fullSource(WebDoc);
end;
WebDoc is an ihtmldocument2.
For each frame (note there can be frames within
frames) you get WebDoc. I use this to build a
string, sPageSource.
Code Completion will offer the properties of
ihtmldocument2 when you type 'webdoc.'. You
probably want to select from:
For Javascript:
Scriptcol := WebDoc.Get_scripts; //all the
javascript tags
ScriptLen := Scriptcol.Get_length;
for r := 0 to ScriptLen - 1 do
begin //get JavaScript
docdisp := ScriptCol.item(r, 0);
OleCheck(docdisp.QueryInterface(IID_IHTMLScriptEle
ment, ScriptEl));
sPageSource := sPageSource +
Trim(ScriptEl.Get_text) + #13#10;
end;
For the HTML
sPageSource := sPageSource +
WebDoc.body.outerHTML;
Nick Tulett
"Rik Barker" <rik.b...@visionsoft.com> wrote in message
news:3b1f8b97_2@dnews...
If you imported the mshtml type library, you should have gained a
TWebBrowser component in the VCL that you can drop on your form.
webBrowser: TWebBrowser
Then you don't need the shellwindow, just WebPage := webBrowser.document as
ihtmldocument2.
Use the documentcomplete event and test for webBrowser.readystatus = 4 to
know when the page has been completely downloaded.
"Rik Barker" <rik.b...@visionsoft.com> wrote in message
news:3b1f8b97_2@dnews...
I'm using IShellWindows because I'm not after the source of my own
instantiation of a Browser, I'm after any open Browser windows.
I also don't want to use OnDocumentComplete (partly because I don't think
it's possible to tie an event to another app without getting into
subclassing etc), and partly because I want to check the source before it's
finished loading in some instances.
Thanks for your tip, it explains a lot about where I've been going wrong.
My main trouble with this has been the lack of documentation explaining
what's returned and what I can do with it. Being told that
WebBrowse.Document returns as IDispatch (the help file) is about as useful
as saying "and it returns something". *8)
Rik.
BugHunta <BugH...@btinternet.com> wrote in message news:3b221c1d_2@dnews...