--------------------------------------------------------
===================================
unit Unit1;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls, OleCtrls, SHDocVw,mshtml, AppEvnts;
type
TForm1 = class(TForm)
WebBrowser1: TWebBrowser;
Button1: TButton;
ApplicationEvents1: TApplicationEvents;
procedure WebBrowser1DocumentComplete(Sender: TObject;
const pDisp: IDispatch; var URL: OleVariant);
procedure Button1Click(Sender: TObject);
procedure ApplicationEvents1Message(var Msg: tagMSG;
var Handled: Boolean);
private
{ Private declarations }
public
{ Public declarations }
end;
var
Form1: TForm1;
implementation
{$R *.dfm}
procedure TForm1.WebBrowser1DocumentComplete(Sender: TObject;
const pDisp: IDispatch; var URL: OleVariant);
begin
tag:=1;
end;
procedure TForm1.Button1Click(Sender: TObject);
var
a: IHTMLDocument2;
begin
tag := 0;
WebBrowser1.Navigate('http://www.baidu.com');
while(tag=0) do Application.ProcessMessages;
WebBrowser1.oleobject.Document.body.Scroll := 'no';
a :=WebBrowser1.Document as IHTMLDocument2;
a.Get_ParentWindow.ScrollBy(0,20);
end;
procedure TForm1.ApplicationEvents1Message(var Msg: tagMSG;
var Handled: Boolean);
begin
if (Msg.message = wm_rbuttondown) or (Msg.message = wm_rbuttonup) or
(msg.message = WM_RBUTTONDBLCLK) then
begin
if IsChild(Webbrowser1.Handle, Msg.hwnd) then
Handled := true;
end;
end;
end.
=================================================================================
delphi 对网页自动操作
uses mshtml,IdHTTP;
procedure TFrmmain.Act_BeginWrite;
var
myitem:Olevariant;
i:integer;
tmp:string;
TemName:String;
TemIniFile:TIniFile;
begin
Try
myitem := WebBrowser1.Document;
for i := 0 to myitem.all.length - 1 do
begin
if myitem.all.item(i).tagName = 'INPUT' then
begin
if Uppercase(myitem.all.item(i).type)='TEXT' then //文本框
begin
TemName:=Act_GetName+RANDOMStr(1);
if Uppercase(myitem.all.item(i).name)='USERNAME' then
begin
myitem.all.item(i).focus;
myitem.all.item(i).value:=TemName;
TemIniFile:=TIniFile.Create(ExtractFilePath(ParamStr(0))+'name.ini');
TemIniFile.WriteString(FormatDateTime('YYYY-MM-DD HH:mm:ss',now),'Name',TemName);
TemIniFile.Free;
end;
end;
end;
end;
keybd_event(VK_SPACE,MapVirtualKey(VK_SPACE,0),0,0);
keybd_event(VK_SPACE,MapVirtualKey(VK_SPACE,0),KEYEVENTF_KEYUP,0);
keybd_event(VK_TAB,MapVirtualKey(VK_TAB,0),0,0);
keybd_event(VK_TAB,MapVirtualKey(VK_TAB,0),KEYEVENTF_KEYUP,0);
for i := 0 to myitem.all.length - 1 do
begin
if myitem.all.item(i).tagName = 'INPUT' then
begin
IF Uppercase(myitem.all.item(i).type)='RADIO' then //选择一个
begin
If Uppercase(myitem.all.item(i).name)='RADIOBUTTON' then
begin
myitem.all.item(i).checked :=true;
if FYX_GetRanDom(1)= 1 then
break;
end;
end;
end;
end;
for i := 0 to myitem.all.length - 1 do
begin
if myitem.all.item(i).tagName = 'SELECT' then
begin
if Uppercase(myitem.all.item(i).id)='DRPPID' then //文本框
begin
if Uppercase(myitem.all.item(i).name)='DRPPID' then
begin
if myitem.all.item(i).tagName = 'OPTION' then
myitem.all.item(i).selected:='SELECTED';
end;
end;
end;
end;
sleep(1000);
for i:= 0 to myitem.all.length - 1 do
begin
if myitem.all.item(i).tagName = 'INPUT' then
begin
if Uppercase(myitem.all.item(i).type)='TEXT' then //文本框
begin
If Uppercase(myitem.all.item(i).name)='USERNICK' then
begin
myitem.all.item(i).value:=Act_GetName;
end;
end;
end;
end;
for i:=0 to myitem.all.length - 1 do
begin
if myitem.all.item(i).tagName = 'INPUT' then
begin
if Uppercase(myitem.all.item(i).type)='TEXT' then //文本框
begin
If Uppercase(myitem.all.item(i).name)='TXTCHECKNUM' then
myitem.all.item(i).value:=FYX_GetCodeID;
end;
end;
end;
for i:=0 to myitem.all.length - 1 do
begin
if myitem.all.item(i).tagName = 'INPUT' then
begin
IF Uppercase(myitem.all.item(i).type)='PASSWORD' then //文本框
begin
If Uppercase(myitem.all.item(i).name)='PASSWORD1' then
myitem.all.item(i).value:=1234567;
end;
end;
end;
for i := 0 to myitem.all.length - 1 do
begin
if myitem.all.item(i).tagName = 'INPUT' then
begin
IF Uppercase(myitem.all.item(i).type)='PASSWORD' then //文本框
begin
If Uppercase(myitem.all.item(i).name)='PASSWORD_CNF' then
myitem.all.item(i).value:=1234567;
end;
end;
end;
((WebBrowser1.Document as IHTMLDocument2).all.item('selYear', 0) as
IHTMLSelectElement).selectedIndex := FYX_GetRanDomYear(35);
((WebBrowser1.Document as IHTMLDocument2).all.item('selMonth', 0) as
IHTMLSelectElement).selectedIndex := FYX_GetRanDom(13);
((WebBrowser1.Document as IHTMLDocument2).all.item('selDay', 0) as
IHTMLSelectElement).selectedIndex := FYX_GetRanDom(30);
((WebBrowser1.Document as IHTMLDocument2).all.item('drpPid', 0) as
IHTMLSelectElement).selectedIndex := FYX_GetRanDom(30);
tmp := 'GetCity(''drpPid'',''drpCity'')';
WebBrowser1.OleObject.Document.parentWindow.execScript(tmp,'JavaScript');
((WebBrowser1.Document as IHTMLDocument2).all.item('drpCity', 0) as
IHTMLSelectElement).selectedIndex := FYX_GetRanDom(5)-1;
for i := 0 to myitem.all.length - 1 do
begin
if myitem.all.item(i).tagName = 'INPUT' then
begin
if Uppercase(myitem.all.item(i).type)='CHECKBOX' then //Checkbox
begin
If Uppercase(myitem.all.item(i).name)='CHKAGREEMENT' then
myitem.all.item(i).checked :=true;
end;
end;
end;
//showmessage('OK');
for i := 0 to myitem.all.length - 1 do
begin
if myitem.all.item(i).tagName = 'INPUT' then
begin
if Uppercase(myitem.all.item(i).type)='SUBMIT' then //找按钮点击
begin
myitem.all.item(i).click;
end;
end;
end; //Tagname = Input
Except
end;
TemNum:=TemNum-1;
EdNum.Text:=IntToStr(TemNum);
SumNum:=SumNum+1;
if TemNum <= 0 then
Close;
Timer4.Enabled:=True;
end;
-------------------------------------------------------------
//获取源码
function WB_GetHTMLCode(WebBrowser: TWebBrowser; ACode: TStrings): Boolean;
var
ps: IPersistStreamInit;
ss: TStringStream;
sa: IStream;
s: string;
begin
ps := WebBrowser.Document as IPersistStreamInit;
s := '';
ss := TStringStream.Create(s);
try
sa := TStreamAdapter.Create(ss, soReference) as IStream;
Result := Succeeded(ps.Save(sa, True));
if Result then ACode.Add(ss.Datastring);
finally
ss.Free;
end;
end;
procedure TForm1.Button2Click(Sender: TObject);
begin
Memo1.Lines.Clear;
WB_GetHTMLCode(Webbrowser1, Memo1.Lines);
end;