So, hab den Code mal ein bißchen optimiert, warn noch nen paar Fehlerchen drin.
Jetzt ist es auch egal, ob das <form> einen Namen hat oder nicht:
Delphi-Quellcode:
function Login(AKennung, APasswort: string; ATitle : string = ''): Boolean;
type
TIEFields = record
tagName : String;
fType : String;
end;
const
MaxIEFields = 2;
IEFields: array[1..MaxIEFields] of TIEFields = (
(tagName : 'INPUT'; fType : 'text'),
(tagName : 'INPUT'; fType : 'password')
);
var
ShellWindow: IShellWindows;
WB: IWebbrowser2;
spDisp: IDispatch;
IDoc1: IHTMLDocument2;
Document: Variant;
k, m: Integer;
ovElements: OleVariant;
i: Integer;
IEFieldsCounter: Integer;
IEFormName : array[1..MaxIEFields] Of String;
begin
result := false;
ShellWindow := CoShellWindows.Create;
// get the running instance of Internet Explorer
for k := 0 to ShellWindow.Count do
begin
spDisp := ShellWindow.Item(k);
if spDisp = nil then Continue;
// QueryInterface determines if an interface can be used with an object
spDisp.QueryInterface(iWebBrowser2, WB);
IEFieldsCounter := 0;
if WB <> nil then
begin
WB.Document.QueryInterface(IHTMLDocument2, iDoc1);
if iDoc1 <> nil then
begin
WB := ShellWindow.Item(k) as IWebbrowser2;
Document := WB.Document;
// if page title ok...
if (Pos(ATitle, Document.Title) <> 0) or (ATitle = '') then
// count forms on document and iterate through its forms
for m := 0 to Document.forms.Length - 1 do
begin
ovElements := Document.forms.Item(m).elements;
// iterate through elements
for i := ovElements.Length - 1 downto 0 do
begin
try
// if input fields found, try to fill them out
if (ovElements.item(i).tagName = IEFields[1].tagName) and
(ovElements.item(i).type = IEFields[1].fType) then
begin
ovElements.item(i).Value := AKennung;
IEFormName[1] := Document.forms.Item(m).name;
If (IEFormName[1] = '') then
IEFormName[1] := 'forms[' + intTosTr(m) + ']';
inc(IEFieldsCounter);
end;
if (ovElements.item(i).tagName = IEFields[2].tagName) and
(ovElements.item(i).type = IEFields[2].fType) then
begin
ovElements.item(i).Value := APasswort;
IEFormName[2] := Document.forms.Item(m).name;
If (IEFormName[2] = '') then
IEFormName[2] := 'forms[' + intTosTr(m) + ']';
inc(IEFieldsCounter);
end;
except
// failed...
end;
end; { for i...}
end; { for m }
end; { idoc <> nil }
end; { wb <> nil }
// if the fields are filled in, submit.
if (IEFieldsCounter = MaxIEFields) and (IEFormName[1] = IEFormName[2]) then
begin
ExecuteScript(iDoc1, 'document.' + IEFormName[1] + '.submit()', 'JavaScript');
result := true;
end;
end; { for k }
end;