代码:unit Unit1;interfaceuses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, ExtCtrls, ImgList, StdCtrls, OleCtrls, SHDocVw,MSHTML,WinInet;type
TForm1 = class(TForm)
WebBrowser1: TWebBrowser;
Button1: TButton;
procedure Button1Click(Sender: TObject);
procedure FormCreate(Sender: TObject);
private
{ Private declarations }
public
procedure FillIEForm(aValidatecode:String;bPost:boolean=False);
{ Public declarations }
end;var
Form1: TForm1;implementation
uses math;{$R *.dfm}
procedure TForm1.FillIEForm(aValidatecode:String;bPost:boolean=False);
procedure DoWithHtmlElement(aElementCollection:IHTMLElementCollection);
var
k:integer;
vk:oleVariant;
Dispatch: IDispatch;
HTMLInputElement:IHTMLInputElement;
HTMLSelectElement:IHTMLSelectElement;
HTMLOptionElement: IHTMLOptionElement;
HTMLTextAreaElement: IHTMLTextAreaElement;
HTMLFormElement:IHTMLFormElement;
HTMLOptionButtonElement:IHTMLOptionButtonElement;
begin
for k:=0 to aElementCollection.length -1 do
begin
Vk:=k;
Application.ProcessMessages;
Dispatch:=aElementCollection.item(vk,0);
if SUCCEEDED(Dispatch.QueryInterface(IHTMLFormElement,HTMLFormElement))then
begin with HTMLFormElement do//±íµ¥
begin
//´¦Àí
if bPost then
begin
//HTMLFormElement.submit ;
// showmessage('hh'); // exit;
end;
end;
end
else if Succeeded(Dispatch.QueryInterface(IHTMLInputElement,HTMLInputElement)) then
begin With HTMLInputElement do//µ¥ÐÐÎı¾ begin
if (UpperCase(Type_)='TEXT') or (UpperCase(Type_)='PASSWORD') or (UpperCase(Type_)='SUBMIT') then
begin
//value:='qq';
if Name='UserID' then Value:='133'
else if Name='UserPass' then Value:='111'
else if Name='Passwd1' then Value:='123456'
else if Name='ButtonSubmit' then
begin HTMLInputElement.form.submit;
//htmlinputelement.form.submit;
showmessage('hh');
end;
end
else if (UpperCase(Type_)='CHECKBOX') then//¸´Ñ¡¿ò
begin
checked:=true;
end
else if (UpperCase(Type_)='RADIO') then//µ¥Ñ¡¿ò
begin
checked :=true;
end;
end;
end
else if Succeeded(Dispatch.QueryInterface(IHTMLSelectElement,HTMLSelectElement)) then
begin
With HTMLSelectElement do//ÏÂÀ¿ò
begin
selectedIndex :=1;
end;
end
else if Succeeded(Dispatch.QueryInterface(IHTMLTEXTAreaElement,HTMLTextAreaElement)) then
begin
with HTMLTextAreaElement do//¶àÐÐÎı¾
begin
value :='textarea';
end;
end
else if Succeeded(Dispatch.QueryInterface(IHTMLOptionElement,HTMLOptionElement)) then
begin
with HTMLOptionElement do//ÏÂÀÑ¡Ïî
begin
//´¦Àí
end;
end
else
//showmessage('other');
;
end;
end;
var HTMLDocument:IHTMLDocument2;
ElementCollection:IHTMLElementCollection;
begin
HTMLDocument:=IHTMLDocument2(WebBrowser1.Document);
if HTMLDocument<>nil then
begin
if HTMLDocument.frames.length =0 then//ÎÞ¿ò¼Ü
begin
ElementCollection:=HTMLDocument.Get_All;
DoWithHtmlElement(ElementCollection); end; end;
end;procedure TForm1.Button1Click(Sender: TObject);begin
while WebBrowser1.ReadyState <READYSTATE_COMPLETE do
Application.ProcessMessages;
FillIEForm('');WebBrowser1.Navigate('10.201.10.106/web/system/condition/selchoice.aspx?int_id=3249&url=pmreport/rep2006/ÖйúÁªÍ¨GSMÍøÒƶ¯Í¨ÐÅÐÔÄܱ¨±í4');end;procedure TForm1.FormCreate(Sender: TObject);
begin
WebBrowser1.Navigate('10.201.10.106/web');
end;end.
////////////HTMLInputElement.form.submit 报错 access violation 各位大大帮帮忙啊
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, ExtCtrls, ImgList, StdCtrls, OleCtrls, SHDocVw,MSHTML,WinInet;type
TForm1 = class(TForm)
WebBrowser1: TWebBrowser;
Button1: TButton;
procedure Button1Click(Sender: TObject);
procedure FormCreate(Sender: TObject);
private
{ Private declarations }
public
procedure FillIEForm(aValidatecode:String;bPost:boolean=False);
{ Public declarations }
end;var
Form1: TForm1;implementation
uses math;{$R *.dfm}
procedure TForm1.FillIEForm(aValidatecode:String;bPost:boolean=False);
procedure DoWithHtmlElement(aElementCollection:IHTMLElementCollection);
var
k:integer;
vk:oleVariant;
Dispatch: IDispatch;
HTMLInputElement:IHTMLInputElement;
HTMLSelectElement:IHTMLSelectElement;
HTMLOptionElement: IHTMLOptionElement;
HTMLTextAreaElement: IHTMLTextAreaElement;
HTMLFormElement:IHTMLFormElement;
HTMLOptionButtonElement:IHTMLOptionButtonElement;
begin
for k:=0 to aElementCollection.length -1 do
begin
Vk:=k;
Application.ProcessMessages;
Dispatch:=aElementCollection.item(vk,0);
if SUCCEEDED(Dispatch.QueryInterface(IHTMLFormElement,HTMLFormElement))then
begin with HTMLFormElement do//±íµ¥
begin
//´¦Àí
if bPost then
begin
//HTMLFormElement.submit ;
// showmessage('hh'); // exit;
end;
end;
end
else if Succeeded(Dispatch.QueryInterface(IHTMLInputElement,HTMLInputElement)) then
begin With HTMLInputElement do//µ¥ÐÐÎı¾ begin
if (UpperCase(Type_)='TEXT') or (UpperCase(Type_)='PASSWORD') or (UpperCase(Type_)='SUBMIT') then
begin
//value:='qq';
if Name='UserID' then Value:='133'
else if Name='UserPass' then Value:='111'
else if Name='Passwd1' then Value:='123456'
else if Name='ButtonSubmit' then
begin HTMLInputElement.form.submit;
//htmlinputelement.form.submit;
showmessage('hh');
end;
end
else if (UpperCase(Type_)='CHECKBOX') then//¸´Ñ¡¿ò
begin
checked:=true;
end
else if (UpperCase(Type_)='RADIO') then//µ¥Ñ¡¿ò
begin
checked :=true;
end;
end;
end
else if Succeeded(Dispatch.QueryInterface(IHTMLSelectElement,HTMLSelectElement)) then
begin
With HTMLSelectElement do//ÏÂÀ¿ò
begin
selectedIndex :=1;
end;
end
else if Succeeded(Dispatch.QueryInterface(IHTMLTEXTAreaElement,HTMLTextAreaElement)) then
begin
with HTMLTextAreaElement do//¶àÐÐÎı¾
begin
value :='textarea';
end;
end
else if Succeeded(Dispatch.QueryInterface(IHTMLOptionElement,HTMLOptionElement)) then
begin
with HTMLOptionElement do//ÏÂÀÑ¡Ïî
begin
//´¦Àí
end;
end
else
//showmessage('other');
;
end;
end;
var HTMLDocument:IHTMLDocument2;
ElementCollection:IHTMLElementCollection;
begin
HTMLDocument:=IHTMLDocument2(WebBrowser1.Document);
if HTMLDocument<>nil then
begin
if HTMLDocument.frames.length =0 then//ÎÞ¿ò¼Ü
begin
ElementCollection:=HTMLDocument.Get_All;
DoWithHtmlElement(ElementCollection); end; end;
end;procedure TForm1.Button1Click(Sender: TObject);begin
while WebBrowser1.ReadyState <READYSTATE_COMPLETE do
Application.ProcessMessages;
FillIEForm('');WebBrowser1.Navigate('10.201.10.106/web/system/condition/selchoice.aspx?int_id=3249&url=pmreport/rep2006/ÖйúÁªÍ¨GSMÍøÒƶ¯Í¨ÐÅÐÔÄܱ¨±í4');end;procedure TForm1.FormCreate(Sender: TObject);
begin
WebBrowser1.Navigate('10.201.10.106/web');
end;end.
////////////HTMLInputElement.form.submit 报错 access violation 各位大大帮帮忙啊
但是程序执行的时候前面 用户名和密码都填写了。然后页面刷新一下就报错。 开始怀疑是表单对象没了。不用HTMLInputElement.form.submit 用 HTMLInputElement.form.name就不会报错,
form : IHTMLFormElement;
...
form := Doc.all.item('Form1',0) as IHTMLFormelement;
form.submit;