TWebBrowser的小问题!为什么这样情况下WebBrowserDocumentComplete得不到触发? (30分)

  • 主题发起人 主题发起人 坏蛋是我
  • 开始时间 开始时间

坏蛋是我

Unregistered / Unconfirmed
GUEST, unregistred user!
var
这样的代码总是不行!

TextFileName: string;

procedure TMainForm.OpenUrlBtnClick(Sender: TObject);
var
FileAttrs, i: Integer;
searchRec: TsearchRec;
HtmlName, mainpath: string;
begin
mainpath:= 'E:/html/';
if FindFirst(mainpath+'*.htm',FileAttrs, SearchRec)=0 then
begin
HtmlName:= mainpath+SearchRec.Name;
TextFileName:= Copy(HtmlName, 1, strlen(pchar(HtmlName))-3)+'txt';
WebBrowser.Navigate(HtmlName);
while FindNext(SearchRec) = 0 do
begin
HtmlName:= mainpath+SearchRec.Name;
TextFileName:= Copy(HtmlName, 1, strlen(pchar(HtmlName))-3)+'txt';
WebBrowser.Navigate(HtmlName);
end;
FindClose(SearchRec);
end;


end;

procedure TMainForm.WebBrowserDocumentComplete(Sender: TObject;
const pDisp: IDispatch; var URL: OleVariant);
var
buffer: string;
DataFile: TFileStream;
HTMLDocument: IHTMLDocument2;
begin
HTMLDocument := WebBrowser.Document as IHTMLDocument2;
DataFile:= TFileStream.Create(TextFileName, fmCreate);
strlen(pchar(HTMLDocument.body.innerText));
buffer:= HTMLDocument.body.innerText;
DataFile.Write(buffer, strlen(pchar(buffer)));
DataFile.Free;
end;

如果改成就可以?
procedure TMainForm.OpenUrlBtnClick(Sender: TObject);
begin
WebBrowser.Navigate(UrlEdit.Text);
end;

procedure TMainForm.SaveAsBtnClick(Sender: TObject);
var
HTMLDocument: IHTMLDocument2;
begin
HTMLDocument := WebBrowser.Document as IHTMLDocument2;
Memo1.Text := HTMLDocument.body.innerText;
end;

 
必须在页面载入后引用 HTMLDocument.body.innerText
WebBrowser1Complete事件
 
请注意你的sleep(10000)把程序挂起了,也就是说,白等了10秒钟[:D]
改成这样试试:
WebBrowser.Navigate(UrlEdit.Text);
while WebBrowser.ReadyState<>READYSTATE_COMPLETE do Application.ProcessMessages;
HTMLDocument := WebBrowser.Document as IHTMLDocument2;
Memo1.Text := HTMLDocument.body.innerText;
 
解决了,谢谢楼上两位!
 
多人接受答案了。
 
后退
顶部