Einzelnen Beitrag anzeigen

MrKnogge

Registriert seit: 9. Jun 2003
Ort: Pforzheim
2.458 Beiträge
 
Delphi 2007 Professional
 
#1

Formular im IExplorer ausfüllen

  Alt 15. Nov 2003, 15:35
Servus !

ja, ich weiss Threads mit dieser Überschrift gibts dutzende,
allerdings wurde ich aus ihnen nicht schlau, oder sie bezogen
sich auf die TWebbrowser Komponennte.

Ich hab einen Code gefunden, um Formulare
im IExplorer auszufüllen, nun hab ich aber ein Problem:

Delphi-Quellcode:
function Login(AKennung, APasswort: string): Boolean;
const
  IEFields: array[1..4] of string = ('INPUT', 'text', 'INPUT', 'password');
var
  ShellWindow: IShellWindows;
  WB: IWebbrowser2;
  spDisp: IDispatch;
  IDoc1: IHTMLDocument2;
  Document: Variant;
  k, m: Integer;
  ovElements: OleVariant;
  i: Integer;
  IEFieldsCounter: Integer;
begin
  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);
    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 Pos('', Document.Title) <> 0 then
          // count forms on document and iterate through its forms
          IEFieldsCounter := 0;
          for m := 0 to Document.forms.Length - 1 do
          // Document.forms.Length ist 0 und somit wird die schleife nie durchlaufen
          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]) and
                  (ovElements.item(i).type = IEFields[2]) then
                  begin
                    ovElements.item(i).Value := AKennung;
                    inc(IEFieldsCounter);
                  end;

                if (ovElements.item(i).tagName = IEFields[3]) and
                  (ovElements.item(i).type = IEFields[4]) then
                  begin
                    ovElements.item(i).Value := APasswort;
                    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 = 3 then ExecuteScript(iDoc1, 'document.login.submit()', 'JavaScript');
  end; { for k }
end;


Die Website sieht so aus:

Code:
<form action="/index.php?action=login" method=POST>

Benutzername:
<input type="text" name="member">

Passwort:
<input type="password" name="pass">

Server:
<select name="server">
  <option value="1">Server 1</option>
  <option value="2">Server 2</option>
</select>

<input type="submit" value="anmelden">

</form>
Warum funktioniert der obige Code hier nicht ?
Christian Bootz
Einstein ist tot, Newton ist tot,
und mir ist auch schon ganz schlecht...
  Mit Zitat antworten Zitat