to ly_liuyang(Liu Yang): 应用程序更好,能不能说详细点呢,我还没什么思路呢.
automatic fill out HTML forms with TWebBrowser? { To test this code put a TWebBrowser and A TButton component on the form } function FillForm(WebBrowser: TWebBrowser; FieldName: string; Value: string): Boolean; var i, j: Integer; FormItem: Variant; begin Result := False; //no form on document if WebBrowser.OleObject.Document.all.tags('FORM').Length = 0 then begin Exit; end; //count forms on document for I := 0 to WebBrowser.OleObject.Document.forms.Length - 1 do begin FormItem := WebBrowser.OleObject.Document.forms.Item(I); for j := 0 to FormItem.Length - 1 do begin try //when the fieldname is found, try to fill out if FormItem.Item(j).Name = FieldName then begin FormItem.Item(j).Value := Value; Result := True; end; except Exit; end; end; end; end; //When the document is complete try to fill out the field homepage with the url procedure TForm1.WebBrowser1DocumentComplete(Sender: TObject; const pDisp: IDispatch; var URL: OleVariant); begin if FillForm(WebBrowser1, 'homepage', 'http://www.swissdelphicenter.ch') = False then ShowMessage('Error. Field not available or no Form found.'); end;// Show the Webbrowser-Progress in Label1 procedure TForm1.WebBrowser1ProgressChange(Sender: TObject; Progress, ProgressMax: Integer); begin if ProgressMax = 0 then begin label1.Caption := ''; Exit; end; try if (Progress <> -1) and (Progress <= ProgressMax) then label1.Caption := IntToStr((Progress * 100) div ProgressMax) + '% loaded...' else label1.Caption := ''; except on EDivByZero do Exit; end; end; //For example you can load the page /en/addtip.php to the TWebBrowser //When the document is Complete the form where you can put your homepage //address is filled out procedure TForm1.Button1Click(Sender: TObject); begin Webbrowser1.Navigate('http://www.swissdelphicenter.ch/en/addtip.php'); // Show the Titel of the currently active Webpage in the titlebar // Den Titel der aktuellen Webseite in der Titeleiste anzeigen Caption := Webbrowser1.OleObject.Document.Title; end;
用控件,参数都为字符型可保存在文件中如网站地址,ID,PW,便于修改
非常感谢 aiirii(ari-求职广州中)!!表单填写后怎么自动提交呢?怎么获取页面的数据呢?
Uses TeeURL;procedure TForm1.Button1Click(Sender: TObject); Var MyStream : TMemoryStream; ErrorCode : HResult; begin MyStream := TMemoryStream.Create; try ErrorCode:= DownloadURL('http://www.steema.com/demo.txt',MyStream); if ErrorCode=0 then Memo1.Lines.LoadFromStream(MyStream) else ShowMessage( TeeURLErrorMessage( ErrorCode ) ); finally MyStream.Free; end;end;
其二.用webservice.服务器提供webservice服务,客户端连上后获取数据(xml格式).
因为有Cookies和Session
自己处理很复杂的如果使用Twebbrowser就好办些的,IE内核嘛http://lysoft.7u7.net
function FillForm(WebBrowser: TWebBrowser; FieldName: string; Value: string): Boolean;
var
i, j: Integer;
FormItem: Variant;
begin
Result := False;
//no form on document
if WebBrowser.OleObject.Document.all.tags('FORM').Length = 0 then
begin
Exit;
end;
//count forms on document
for I := 0 to WebBrowser.OleObject.Document.forms.Length - 1 do
begin
FormItem := WebBrowser.OleObject.Document.forms.Item(I);
for j := 0 to FormItem.Length - 1 do
begin
try
//when the fieldname is found, try to fill out
if FormItem.Item(j).Name = FieldName then
begin
FormItem.Item(j).Value := Value;
Result := True;
end;
except
Exit;
end;
end;
end;
end;
//When the document is complete try to fill out the field homepage with the url
procedure TForm1.WebBrowser1DocumentComplete(Sender: TObject;
const pDisp: IDispatch; var URL: OleVariant);
begin
if FillForm(WebBrowser1, 'homepage', 'http://www.swissdelphicenter.ch') = False then
ShowMessage('Error. Field not available or no Form found.');
end;// Show the Webbrowser-Progress in Label1
procedure TForm1.WebBrowser1ProgressChange(Sender: TObject; Progress, ProgressMax: Integer);
begin
if ProgressMax = 0 then
begin
label1.Caption := '';
Exit;
end;
try
if (Progress <> -1) and (Progress <= ProgressMax) then
label1.Caption := IntToStr((Progress * 100) div ProgressMax) + '% loaded...'
else
label1.Caption := '';
except
on EDivByZero do Exit;
end;
end;
//For example you can load the page /en/addtip.php to the TWebBrowser
//When the document is Complete the form where you can put your homepage
//address is filled out
procedure TForm1.Button1Click(Sender: TObject);
begin
Webbrowser1.Navigate('http://www.swissdelphicenter.ch/en/addtip.php');
// Show the Titel of the currently active Webpage in the titlebar
// Den Titel der aktuellen Webseite in der Titeleiste anzeigen
Caption := Webbrowser1.OleObject.Document.Title;
end;
Var MyStream : TMemoryStream;
ErrorCode : HResult;
begin
MyStream := TMemoryStream.Create;
try
ErrorCode:= DownloadURL('http://www.steema.com/demo.txt',MyStream);
if ErrorCode=0 then
Memo1.Lines.LoadFromStream(MyStream)
else
ShowMessage( TeeURLErrorMessage( ErrorCode ) );
finally
MyStream.Free;
end;end;