网上找了一堆,没有找到Frame嵌套情况的。
Frame里面的东西,相当一个独立的网页。它的JavaScript环境是独立的,变量是受保护的。所以,要先找到具体的Frame,然后再执行脚本。
这段代码使用了递归。抛砖引玉。
var
doc: IHTMLDocument2;
ICount: Integer;
procedure showFrame(doc: IHTMLDocument2);
var
framedoc: IHTMLDocument2;
i, FCount: Integer;
frame_dispatch: IDispatch;
ole_index: oleVariant;
oo: IHTMLElement;
II: IHTMLInputElement;
btn: IHTMLFormElement;
begin
if doc = nil then
Exit;
inc(ICount);
FCount := ICount;
Memo2.Lines.Add('进入次数' + FCount.ToString);
for i := 0 to doc.frames.length - 1 do //遍历所有Frame
begin
ole_index := i;
frame_dispatch := doc.frames.item(ole_index);
if frame_dispatch = nil then
continue;
Memo2.Lines.Add
('--------------------------------------------------------------------');
Memo2.Lines.Add(i.ToString);
// memo2.Lines.Add(frame_dispatch);
framedoc := (frame_dispatch as IHTMLWindow2).Document as IHTMLDocument2;
if framedoc = nil then
continue;
Memo2.Lines.Add(framedoc.url);
if System.Pos('http://yuhuan.zjjgbz.gov.cn/admin/content/',
pchar(framedoc.url)) > 0 then
begin
II := framedoc.all.item('title', 0) as IHTMLInputElement; //填写标题
if II <> nil then
II.value := ListView1.Items[FItemID].SubItems[0];
oo := framedoc.all.item('txt', 0) as IHTMLElement; //以这个为标志,找到Frame,执行脚本
if oo <> nil then
begin
try
framedoc.parentWindow.ExecScript(Memo1.Text, 'javascript');
Memo2.Lines.Add('OK执行!脚本。。。。。。。。。。。。。。。。。。。。。。。。。');
except
on e: exception do
begin
Memo2.Lines.Add('出错!;;;;;;;;;;;;' + e.Message);
Memo2.Lines.Add(framedoc.body.innerHTML);
end;
end;
// try
// sleep(500);
// btn := framedoc.all.item('jvform', 0) as IHTMLFormElement;
// if btn = nil then
// begin
// Memo2.Lines.Add('not find jvForm........');
// end
// else
// begin
// Memo2.Lines.Add('find form ok......................!');
// btn.submit;
// end;
// except
//
// end;
end
else
begin
Memo2.Lines.Add('没找到TXT..................................');
end;
end;
Memo2.Lines.Add('---------------');
// memo2.Lines.Add(oo.name+'-------------');
showFrame(framedoc);
Memo2.Lines.Add('退出次数' + FCount.ToString);
end;
end;
begin
Memo2.Clear;
doc := WebBrowser1.Document as IHTMLDocument2;
ActionCopyWord.Execute;
showFrame(doc);
Application.ProcessMessages;