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
581 Views
Last Modified: 2007-12-19
0
Comment
Question by:mikekwok
  • 14
  • 9
23 Comments
 
LVL 5

Expert Comment

by:fantasy1001
Comment Utility
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
Comment Utility
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
Comment Utility
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
 
LVL 5

Expert Comment

by:fantasy1001
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Free Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

 
LVL 5

Expert Comment

by:fantasy1001
Comment Utility
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
Comment Utility
Would somebody pls tell me how can I detect if Internet Explorer finsihed loading a homepage in Visual Basic ?
0
 

Author Comment

by:mikekwok
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
I wish I could help. But that was outside my ability.
0
 

Author Comment

by:mikekwok
Comment Utility
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
Comment Utility
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
Comment Utility
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

Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

Join & Write a Comment

Article by: Martin
Here are a few simple, working, games that you can use as-is or as the basis for your own games. Tic-Tac-Toe This is one of the simplest of all games.   The game allows for a choice of who goes first and keeps track of the number of wins for…
You can of course define an array to hold data that is of a particular type like an array of Strings to hold customer names or an array of Doubles to hold customer sales, but what do you do if you want to coordinate that data? This article describes…
As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
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…

771 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

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now