Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people, just like you, are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
Solved

writing a program to insert text in a form and click "submit" button in a browser (IE or Netscape)?

Posted on 2003-10-24
23
585 Views
Last Modified: 2007-12-19
0
Comment
Question by:mikekwok
  • 14
  • 9
23 Comments
 
LVL 5

Expert Comment

by:fantasy1001
ID: 9618729
Using VB, you can do this kind of stuff.

Get a handler to the IE/Netscape using FindWindow.
Then, use Spy++ to find the class or name of the form textbox, for example "edit", and also the submit button.
Ok, then use sendmessage to send the text and then the submit event to the ie.

Second way,
activate ie/netscape using AppActivate
use keybd_event to send keystroke to the form textbox,
for example: to send character 'A' to the textbox
keybd_event vbKeyA, 0, 0, 0
keybd_event vbkeyA, 0, &H2, 0

Thanks & Cheers
0
 

Author Comment

by:mikekwok
ID: 9618740
would you tell me more details on the "first way" ? Would you tell me how to get a handler to IE using FindWindow  and the Spy++ ? would u send me some tutorials on that the first way ?
0
 

Author Comment

by:mikekwok
ID: 9618765
I have find information on theFindWindow, however, I really get no idea about the Spy++ , would you please tell me more details on that ?Thanks
0
Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
LVL 5

Expert Comment

by:fantasy1001
ID: 9618772
We can get a handler to a window using the FindWindow API.
First, from Spy++, you find the IE window class name, or if you have the window title for example:
dim hwnd as object
hwnd = findwindow(classname, window title)

ref: http://www.mentalis.org/apilist/FindWindow.shtml

Then from here, use Spy++ to locate the textbox on the form. Normally, we can only get the control class name which is in default "Edit",. So now use findwindowex
dim hwndchild as object
hwndchild = findwindowex hwnd, 0&, "Edit", vbnullstring

Ok, then use sendmessage hwndchild, SETTEXT, 256, "your text" to send the text to the textbox.
To trigger the Enter button, also use sendmessage.
Cannot help out much because oop

Thanks & Cheers
0
 
LVL 5

Expert Comment

by:fantasy1001
ID: 9624671
Sorry for the confusion. There is difficulty if we use the first method where we cannot get the control class name for html control. As for that, I will give you an example on the second method which emulate keyboard event. To try this, first open an IE page to www.google.com, then press the command1 button

Private Declare Function FindWindow Lib "user32" Alias "FindWindowA" (ByVal lpClassName As String, ByVal lpWindowName As String) As Long
Private Declare Function GetWindowText Lib "user32" Alias "GetWindowTextA" (ByVal hwnd As Long, ByVal lpString As String, ByVal cch As Long) As Long
Private Declare Function GetWindowTextLength Lib "user32" Alias "GetWindowTextLengthA" (ByVal hwnd As Long) As Long

Private Declare Sub keybd_event Lib "user32" (ByVal bVk As Byte, ByVal bScan As Byte, ByVal dwFlags As Long, ByVal dwExtraInfo As Long)
Private Const KEYEVENTF_KEYUP = &H2

Private Sub Command1_Click()
    'Dim hwnd As Long
    'Dim caption As String
   
    'hwnd = FindWindow("IEFrame", vbNullString)
    'caption = String(GetWindowTextLength(hwnd) + 1, chr$(0))
    'GetWindowText hwnd, caption, Len(caption)
    AppActivate "Google - Microsoft Internet Explorer"
    SetText ("Enter string here")
End Sub

Function SetText(str As String)
    Dim i As Integer
    Dim chr As String
    For i = 0 To Len(str) - 1
        chr = UCase(Mid(str, i + 1, 1))
        keybd_event Asc(chr), 0, 0, 0
        keybd_event Asc(chr), 0, &H2, 0
    Next
    'press <enter> here
    keybd_event 13, 0, 0, 0
    keybd_event 13, 0, &H2, 0
End Function

## There is others way of doing it, for example, we grab the html and process it first, then only we send it back.

Thanks & Cheers
0
 

Author Comment

by:mikekwok
ID: 9632272
When I copy the Command1_Click into a new form, i have the following error :
AppActivate "Google - Microsoft Internet Explorer"

Invalid procedure call or argument...

I am using Visual Basic 6 but not Visual Basic .Net ... What should I do ?


   
0
 

Author Comment

by:mikekwok
ID: 9632293
I have solved the invalid procedure call or argument .....

We should open an internet explorer before running this line ...
therefore, i try to open an IE when the form load

Private Sub Form_Load()
    Set IE = CreateObject("InternetExplorer.Application")
    IE.Navigate "http://www.google.com"
    IE.Visible = True
    Do While IE.Busy
       DoEvents
    Loop
End Sub
0
 
LVL 5

Expert Comment

by:fantasy1001
ID: 9632314
Ok, nevermind, we can activate the window by its type, make sure there is only one ie window opened. then change here

Private Sub Command1_Click()
    Dim hwnd As Long
    Dim caption As String
   
    hwnd = FindWindow("IEFrame", vbNullString)
    caption = String(GetWindowTextLength(hwnd) + 1, chr$(0))
    GetWindowText hwnd, caption, Len(caption)
    AppActivate caption
    SetText ("Enter string here")
End Sub
0
 

Author Comment

by:mikekwok
ID: 9632317
For example, if there is more than 1 textbox in a form and  there are more than 1 form in the hompage and i want to enter some text in both fields in the 2 forms , how can I locate difference forms in Visual Basic ?
,
0
 
LVL 5

Expert Comment

by:fantasy1001
ID: 9632449
You can use the keybd_event if you do not need a reliable solution.
To change to others fields, just keybd_event the <tab>, &h9 until you reach the wanted field. Then to go to other form, keybd_event the <enter> key and wait until the loading complete.

There are many many way of doing it, easy or hard, is depend on the web site you want to go. You can post your desire web site here, so that we can see which method is better.

thanks & cheers

0
 

Author Comment

by:mikekwok
ID: 9632545
Thanks fantasy1001, but I would like to know how can I locate the form in the homepage ( by using the form name ) so that I can enter the text in the textbox in that form ( by usng the textbox name).
As I am afraid that If the homepage design has changed the layout, my program doesn't work .
Also, if there are more than 1 frame in the homapge, even I use the <tab> keybd_event, I cannot goto the other frame by using the <tab> keybd_event .

Do you have any suggestion on my problem ?
0
 
LVL 5

Expert Comment

by:fantasy1001
ID: 9632570
Can you please post the webpage you are dealing with here?
There are many many others quicker method/solution.
If the layout changed, we probably have to redesign the program, even if you use other methods. Please post the url here?
0
 

Author Comment

by:mikekwok
ID: 9632708
Would somebody pls tell me how can I detect if Internet Explorer finsihed loading a homepage in Visual Basic ?
0
 

Author Comment

by:mikekwok
ID: 9632712
I am just trying to prove if this method can implement on my project, I still dunno the exact homepage .... sorry .......
0
 
LVL 5

Expert Comment

by:fantasy1001
ID: 9638008
To know whether Internet Explorer finished loading or not, you can try to get the caption of the window, if it is still loading, it would be something like this

http://www.mmmm.com - Microsoft Internet Explorer

After finish

Description of the page - Microsoft Internet Explorer

You should parse the html if heavy processing is needed. Try to you webbrowser in your program instead.

About html:
1.   Some web page use url parameter passing only for example:
   http://www.google.com/search?hl=en&ie=UTF-8&oe=UTF-8&q=testword&btnG=Google+Search

If so, then we can pass the parameter directly into the url and send back to the web server.

2.   If we see directly into the html tag, we can actually edit the html file to insert our text, for example:

<input type = text value="yourtext"></input>

Thanks & Cheers
0
 

Author Comment

by:mikekwok
ID: 9638504
You have said: "
To know whether Internet Explorer finished loading or not, you can try to get the caption of the window, if it is still loading, it would be something like this

http://www.mmmm.com - Microsoft Internet Explorer
"

It is not that case. I try to open a web browser and the caption of the window is the same while loading and finish loading.

I think the url parameter passing only method cannot be implemented ........

0
 

Author Comment

by:mikekwok
ID: 9698615
by the way, I would like to ask if there is any method to see the whole DOM structure of the hompage .
U can take a look on my question :

http://www.experts-exchange.com/Web/Web_Languages/Q_20790515.html
0
 
LVL 5

Accepted Solution

by:
fantasy1001 earned 20 total points
ID: 9698634
Sorry for being misleading, auto complete a web form should be like this:
Dim IE As Object
Dim i As Long
Set IE = CreateObject("Internetexplorer.Application")
IE.Visible = True
IE.Navigate ("http://www.google.com/")
Do While Not IE.ReadyState = 4
  DoEvents
Loop
IE.Document.All("q").Value = "my search string"   'this will insert text into the text box
IE.Document.All("btnG").Click   'this will click the submit button

both "q" and "btnG" are the control name and can be found through html tag.
0
 

Author Comment

by:mikekwok
ID: 9698647
Thanks fantasy1001. Do u know how to show the whole DOM strucutre of a homepage ? i want to know this because i am now suffering from looking on the sourcecode of the homepage in the notepad ... the homepage that i want to automate contains recursive frames. It is too difficult for me to trace the element that i want to locate.
0
 
LVL 5

Expert Comment

by:fantasy1001
ID: 9698869
I wish I could help. But that was outside my ability.
0
 

Author Comment

by:mikekwok
ID: 9737426
After I login to a homepage, it pop up a dialog : "Secure Information. This Page contains both secure and non-secure items. Do you want to display the non-secure items ?"
my current approach to solve this problem is to use AppActivate "Security Information" and then press enter using the keysboard event.

Would somebody tell me how can i know when the secure information dialog s pop up and then i do something after pop u p?
0
 

Author Comment

by:mikekwok
ID: 9745697
would you tell me how to change security settings at IE configuration so that the security information dialog will not pop up ?
Also,If there are 2 frames in a homepage, how can it detect if the content of 1 of the frames finish loading in Visual Basic ?
0
 

Author Comment

by:mikekwok
ID: 9794489
Would somebody please tell me how to check Internet Explorer Language Version ( English, Traditional Chinese, etc)  using Visual Basic 6 ? I know how to detect Internet EXplorer Version ... http://www.experts-exchange.com/Programming/Programming_Languages/Visual_Basic/Q_11203796.html
0

Featured Post

Networking for the Cloud Era

Join Microsoft and Riverbed for a discussion and demonstration of enhancements to SteelConnect:
-One-click orchestration and cloud connectivity in Azure environments
-Tight integration of SD-WAN and WAN optimization capabilities
-Scalability and resiliency equal to a data center

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction I needed to skip over some file processing within a For...Next loop in some old production code and wished that VB (classic) had a statement that would drop down to the end of the current iteration, bypassing the statements that were c…
Have you ever wanted to restrict the users input in a textbox to numbers, and while doing that make sure that they can't 'cheat' by pasting in non-numeric text? Of course you can do that with code you write yourself but it's tedious and error-prone …
Get people started with the process of using Access VBA to control Outlook using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Microsoft Outlook. Using automation, an Access applic…
Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…

839 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question