Formular im IExplorer ausfüllen
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:
Warum funktioniert der obige Code hier nicht ?
<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> |
Re: Formular im IExplorer ausfüllen
Erstmal solltest du
Code:
durch das ersetzen (wnn du Einfluss darauf hast):
<form action="/index.php?action=login" method=POST>
Code:
Desweiteren wird diese Abfrage immer fehlschlagen, weil das Ergebnis von Pos bei einem Leerstring immer 0 ist:
<form name="login" action="/index.php?action=login" method=POST>
Delphi-Quellcode:
Da musst du den Title des Fensters (oder einen Teil davon) einsetzen bzw. das ganze durch
if Pos('', Document.Title) <> 0 then
Delphi-Quellcode:
ersetzen.
if true then
|
Re: Formular im IExplorer ausfüllen
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; |
Re: Formular im IExplorer ausfüllen
thx, werds gleich mal testen !
Was den Titel der Website angeht, ich hab '' hingeschrieben, weil die Seitekeinen Titel hat :roll: |
Re: Formular im IExplorer ausfüllen
Zitat:
|
Re: Formular im IExplorer ausfüllen
Zitat:
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. |
Re: Formular im IExplorer ausfüllen
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 :wiejetzt:
Code:
mfg
<select size=1 name=cho>
<option>xxx</option> <option>xxx2</option> <option>xxx3</option> <option>xxx4</option> <option>xxx5</option> <option>xxx6</option> </select> NeoXan |
Re: Formular im IExplorer ausfüllen
Nehmen wir an, du gehts ansonsten wie oben vor und möchtest jetzt in der Combobox "xxx3" auswählen:
Delphi-Quellcode:
Die 2 steht für den Index des Items (der option) in der Combobox (beginnend bei 0).
ovElements.item(i).item(2).Selected := true;
[edit] Oder nach Text:
Delphi-Quellcode:
[/edit]
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; |
Re: Formular im IExplorer ausfüllen
Also, bei mir in Delphi 3 Prof. funzt der Code von scp nicht. :( Delphi kennt fast gar nichts...
Code:
Undefienierter Bezeichner: ...
|
Re: Formular im IExplorer ausfüllen
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)?
|
Alle Zeitangaben in WEZ +1. Es ist jetzt 02:14 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