stuayre
asked on
Send text to current IE window? (auto form fill)
How can I send text to the current IE window?
I have this code, but it's only for webbrowser1, how can I convert it to work with the current IE window?
I want to auto complete a file upload form (see below)
procedure TForm1.Button2Click(Sender : TObject);
var
Doc: IHTMLDocument2;
Elements: IHTMLElementCollection;
Element: IHTMLElement;
InputElement: IHTMLInputElement;
I,j: Integer;
FormItem: Variant;
pch: PChar;
str1: string;
sk:tsendkeys;
IE2: IWebBrowser2;
sh: TShellWindows;
begin
str1:= 'hello';//convert input value to string
pch := PChar(str1);//type cast to pointer
Doc := WebBrowser1.Document as IHTMLDocument2;
//sh := TShellWindows.Create(self) ;
//IE2 := sh.item as IWEbBrowser2;
//doc:=(webbrowser1.docume nt) as ihtmldocument2;
//sh := TShellWindows.Create(self) ;
//IE2 := sh.item as IWEbBrowser2;
//doc:=IE2.Document;
Elements := Doc.all;
for I := 0 to Elements.length - 1 do
begin
Element := Elements.item(I, EmptyParam) as IHtmlElement;
if Element.tagName = 'INPUT' then
begin
InputElement := Element as IHTMLInputElement;
if (SameText(InputElement.typ e_,'file') ) and
(SameText(InputElement.Nam e, 'userfile1[]'))then
begin
FormItem:= InputElement;
FormItem.Focus;
//SendKeys(PCh);// NOTE the false flag
sk.SendKeys(PCh);
end;
end;
end;
end;//SetFilename
the form I am trying to complete is...
<form action="file-upload.php" method="POST" enctype="multipart/form-da ta">
Send these files:<br>
<input name="userfile1[]" type="file"><br>
<input name="userfile2[]" type="file"><br>
<input name="" type="text">
<input type="submit" value="Send files">
</form>
many thnaks
Stu
PS
I've had this responce in VB (from fantasy1001), but I need it in Delphi :(
'This is vb code, very easy
Dim IE As Object
Dim i As Long
Set IE = CreateObject("Internetexpl orer.Appli cation")
IE.Visible = True
IE.Navigate ("http://yourip")
Do While Not IE.ReadyState = 4
DoEvents
Loop
IE.Document.All("userfile1 []").Value = "input text 1"
IE.Document.All("userfile2 []").Value = "input text 2"
IE.Document.All("btnG").Cl ick 'this need the name of the button
see: https://www.experts-exchange.com/Programming/Programming_Languages/Delphi/
I have this code, but it's only for webbrowser1, how can I convert it to work with the current IE window?
I want to auto complete a file upload form (see below)
procedure TForm1.Button2Click(Sender
var
Doc: IHTMLDocument2;
Elements: IHTMLElementCollection;
Element: IHTMLElement;
InputElement: IHTMLInputElement;
I,j: Integer;
FormItem: Variant;
pch: PChar;
str1: string;
sk:tsendkeys;
IE2: IWebBrowser2;
sh: TShellWindows;
begin
str1:= 'hello';//convert input value to string
pch := PChar(str1);//type cast to pointer
Doc := WebBrowser1.Document as IHTMLDocument2;
//sh := TShellWindows.Create(self)
//IE2 := sh.item as IWEbBrowser2;
//doc:=(webbrowser1.docume
//sh := TShellWindows.Create(self)
//IE2 := sh.item as IWEbBrowser2;
//doc:=IE2.Document;
Elements := Doc.all;
for I := 0 to Elements.length - 1 do
begin
Element := Elements.item(I, EmptyParam) as IHtmlElement;
if Element.tagName = 'INPUT' then
begin
InputElement := Element as IHTMLInputElement;
if (SameText(InputElement.typ
(SameText(InputElement.Nam
begin
FormItem:= InputElement;
FormItem.Focus;
//SendKeys(PCh);// NOTE the false flag
sk.SendKeys(PCh);
end;
end;
end;
end;//SetFilename
the form I am trying to complete is...
<form action="file-upload.php" method="POST" enctype="multipart/form-da
Send these files:<br>
<input name="userfile1[]" type="file"><br>
<input name="userfile2[]" type="file"><br>
<input name="" type="text">
<input type="submit" value="Send files">
</form>
many thnaks
Stu
PS
I've had this responce in VB (from fantasy1001), but I need it in Delphi :(
'This is vb code, very easy
Dim IE As Object
Dim i As Long
Set IE = CreateObject("Internetexpl
IE.Visible = True
IE.Navigate ("http://yourip")
Do While Not IE.ReadyState = 4
DoEvents
Loop
IE.Document.All("userfile1
IE.Document.All("userfile2
IE.Document.All("btnG").Cl
see: https://www.experts-exchange.com/Programming/Programming_Languages/Delphi/
Hi Stu,
I really doubt whether that VB code would work because input elements of type 'file' have no Value property that could be set that way. You can start a new IE window this way:
uses ComObj;
var
IE: Variant;
procedure TForm1.Button1Click(Sender : TObject);
begin
IE := CreateOleObject('Internete xplorer.Ap plication' );
IE.Visible := True;
IE.Navigate('http://swift400/sis/login.html');
while not IE.ReadyState = 4 do Application.ProcessMessage s;
// simulate Tab key several times until your first element gets the focus
// here you could simulate your keys for entering the first element
// then Tab key again - next element
// simulate keys for the second element
// Tab key - text element gets the focus
// tab key - the button gets the focus
// simulate Space or Enter key to submit the form
end;
Regards, Geo
I really doubt whether that VB code would work because input elements of type 'file' have no Value property that could be set that way. You can start a new IE window this way:
uses ComObj;
var
IE: Variant;
procedure TForm1.Button1Click(Sender
begin
IE := CreateOleObject('Internete
IE.Visible := True;
IE.Navigate('http://swift400/sis/login.html');
while not IE.ReadyState = 4 do Application.ProcessMessage
// simulate Tab key several times until your first element gets the focus
// here you could simulate your keys for entering the first element
// then Tab key again - next element
// simulate keys for the second element
// Tab key - text element gets the focus
// tab key - the button gets the focus
// simulate Space or Enter key to submit the form
end;
Regards, Geo
ASKER
thanks geo I'll give that a wirl
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Oh, BTW, it's FREE...
ASKER
wow! AutoIT is amazing! thanks Eddie
Yeah, here's how I log in to Hotmail, of course username and password are fictitious.
ShellExecute(0, 'open', PChar('http://login.passport.net/uilogin.srf?id=2'), '', '', SW_SHOW);
AutoIt_WinWaitActive('Plea se sign in', '', 0);
AUTOIT_Sleep(1500);
AutoIt_Send('username@hotm ail.com');
AutoIt_Send('{TAB}');
AutoIt_Send('password');
AutoIt_Send('{ENTER}');
ShellExecute(0, 'open', PChar('http://login.passport.net/uilogin.srf?id=2'), '', '', SW_SHOW);
AutoIt_WinWaitActive('Plea
AUTOIT_Sleep(1500);
AutoIt_Send('username@hotm
AutoIt_Send('{TAB}');
AutoIt_Send('password');
AutoIt_Send('{ENTER}');
ASKER