AGB  ·  Datenschutz  ·  Impressum  







Anmelden
Nützliche Links
Registrieren
Zurück Delphi-PRAXiS Programmierung allgemein Netzwerke Delphi Formular im IExplorer ausfüllen
Thema durchsuchen
Ansicht
Themen-Optionen

Formular im IExplorer ausfüllen

Ein Thema von MrKnogge · begonnen am 15. Nov 2003 · letzter Beitrag vom 24. Jan 2004
Antwort Antwort
Seite 1 von 2  1 2      
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
scp

Registriert seit: 31. Okt 2003
1.120 Beiträge
 
Delphi 7 Personal
 
#2

Re: Formular im IExplorer ausfüllen

  Alt 17. Nov 2003, 00:37
Erstmal solltest du
Code:
<form action="/index.php?action=login" method=POST>
durch das ersetzen (wnn du Einfluss darauf hast):
Code:
<form name="login" action="/index.php?action=login" method=POST>
Desweiteren wird diese Abfrage immer fehlschlagen, weil das Ergebnis von Pos bei einem Leerstring immer 0 ist:
if Pos('', Document.Title) <> 0 then Da musst du den Title des Fensters (oder einen Teil davon) einsetzen bzw. das ganze durch
if true then ersetzen.
  Mit Zitat antworten Zitat
scp

Registriert seit: 31. Okt 2003
1.120 Beiträge
 
Delphi 7 Personal
 
#3

Re: Formular im IExplorer ausfüllen

  Alt 17. Nov 2003, 01:04
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;
  Mit Zitat antworten Zitat
MrKnogge

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

Re: Formular im IExplorer ausfüllen

  Alt 17. Nov 2003, 22:47
thx, werds gleich mal testen !

Was den Titel der Website angeht,
ich hab '' hingeschrieben, weil
die Seitekeinen Titel hat
Christian Bootz
Einstein ist tot, Newton ist tot,
und mir ist auch schon ganz schlecht...
  Mit Zitat antworten Zitat
scp

Registriert seit: 31. Okt 2003
1.120 Beiträge
 
Delphi 7 Personal
 
#5

Re: Formular im IExplorer ausfüllen

  Alt 18. Nov 2003, 00:23
Zitat von MrKnogge:
Was den Titel der Website angeht,
ich hab '' hingeschrieben, weil
die Seitekeinen Titel hat
Jepp, das macht Pos aber leider nicht mit, ist aber ebenfalls bei meiner Variante egal.
  Mit Zitat antworten Zitat
Lars

Registriert seit: 5. Dez 2003
1 Beiträge
 
#6

Re: Formular im IExplorer ausfüllen

  Alt 5. Dez 2003, 20:40
Zitat:
for m := 0 to Document.forms.Length - 1 do
// Document.forms.Length ist 0 und somit wird die schleife nie durchlaufen

Moin!
Ich habe das selbe Problem. (Nur das ich nicht den IE nehme, sondern direkt ein TWebBrowser-Objekt)
Wodurch hast denn nun hinbekommen, dass Document.forms.Length denn was anständiges zurückliefert?


[edit]
Scheint doch so zu gehen!
NUR noch nicht in dem Event OnNavigateComplete2!
Weiß jemand warum?
Oder wie das sonst geht, wenn ich die Seite gerade erst in dem Fenster lade.
  Mit Zitat antworten Zitat
NeoXan
(Gast)

n/a Beiträge
 
#7

Re: Formular im IExplorer ausfüllen

  Alt 19. Jan 2004, 13:53
Und wie könnte ich z.B. zwischen Server 1 und Server 2 wählen...sprich eine Combobox beeinflussen...
Wenn dort Value steht dann geht das...aber was mache ich z.B. hierbei

Code:
          <select size=1 name=cho>
            <option>xxx</option>
            <option>xxx2</option>
            <option>xxx3</option>
            <option>xxx4</option>
            <option>xxx5</option>
            <option>xxx6</option>
          </select>
mfg
NeoXan
  Mit Zitat antworten Zitat
scp

Registriert seit: 31. Okt 2003
1.120 Beiträge
 
Delphi 7 Personal
 
#8

Re: Formular im IExplorer ausfüllen

  Alt 20. Jan 2004, 00:59
Nehmen wir an, du gehts ansonsten wie oben vor und möchtest jetzt in der Combobox "xxx3" auswählen:
ovElements.item(i).item(2).Selected := true; Die 2 steht für den Index des Items (der option) in der Combobox (beginnend bei 0).

[edit]
Oder nach Text:
Delphi-Quellcode:
for x := ovElements.item(i).length-1 downto 0 do
  If (ovElements.item(i).item(x).innerText = 'xxx3') then
    ovElements.item(i).item(x).Selected := true;
[/edit]
  Mit Zitat antworten Zitat
Kinimod8

Registriert seit: 9. Jan 2004
Ort: Barsbüttel
317 Beiträge
 
Delphi 6 Personal
 
#9

Re: Formular im IExplorer ausfüllen

  Alt 23. Jan 2004, 16:36
Also, bei mir in Delphi 3 Prof. funzt der Code von scp nicht. Delphi kennt fast gar nichts...

Code:
Undefienierter Bezeichner: ...
Dominik Peters
  Mit Zitat antworten Zitat
Alexander

Registriert seit: 28. Aug 2002
Ort: Oldenburg
3.513 Beiträge
 
Turbo Delphi für .NET
 
#10

Re: Formular im IExplorer ausfüllen

  Alt 23. Jan 2004, 16:56
Hi hast du auch die ActiveX-Komponenten "Microsoft Internet Control" und "Microsoft HTML Object Library" installiert (Komponente -> ActiveX importieren -> die beiden nach einander auswählen -> Unit erzeugen) und dann eingebunden (MSHTML_TLB, SHDocVw_TLB)?
Alexander
  Mit Zitat antworten Zitat
Antwort Antwort
Seite 1 von 2  1 2      


Forumregeln

Es ist dir nicht erlaubt, neue Themen zu verfassen.
Es ist dir nicht erlaubt, auf Beiträge zu antworten.
Es ist dir nicht erlaubt, Anhänge hochzuladen.
Es ist dir nicht erlaubt, deine Beiträge zu bearbeiten.

BB-Code ist an.
Smileys sind an.
[IMG] Code ist an.
HTML-Code ist aus.
Trackbacks are an
Pingbacks are an
Refbacks are aus

Gehe zu:

Impressum · AGB · Datenschutz · Nach oben
Alle Zeitangaben in WEZ +1. Es ist jetzt 07:59 Uhr.
Powered by vBulletin® Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.
LinkBacks Enabled by vBSEO © 2011, Crawlability, Inc.
Delphi-PRAXiS (c) 2002 - 2023 by Daniel R. Wolf, 2024 by Thomas Breitkreuz