Windows Vista Forums
Vista Forums Home Join Vista Forums Windows 7 Forum Vista Tutorials Tags
Welcome to Windows Vista Forums. Our forum is dedicated to helping you find solutions with any problems, errors or issues you are experiencing with Windows Vista. The Vista forum also covers news and updates and has an extensive Windows Vista tutorial section that covers a wide range of tips and tricks.

Go Back   Vista Forums > Misc Newsgroups > VB Script

Vista Tutorial - Using sendkeys in a web form

Reply
 
Old 08-23-2008   #1 (permalink)
LuisE
Guest


 
 

Using sendkeys in a web form

I need a script to open a website, tab thru a form in that webpage and
"submit" or "enter"at the 11th field (after doing tab for 11 times). I can
get the website open and the window acticvate but in order to be able to tab
inside the page I need to "click" on it. How can I replicate the "click"
event by using SendKeys or other method of VBScript.

Here is what I have

Set wshshell=CreateObject("wscript.shell")

With CreateObject("InternetExplorer.Application")
..Navigate "http://www.hcso.tampa.fl.us/pub/default.asp?/Online/sname01"
..visible=true
End with

Thanks in adavance

My System SpecsSystem Spec
Old 08-23-2008   #2 (permalink)
mr_unreliable
Guest


 
 

Re: Using sendkeys in a web form

LuisE wrote:
Quote:

> I need a script to open a website, tab thru a form in that webpage and
> "submit" or "enter"at the 11th field (after doing tab for 11 times).
hi LuisE,

Instead of using sendkeys, you would be much better off to use
the "document object model" of the page you are navigating to.

There are a number of example scripts posted on this newsgroup,
showing how to do that. In general, you wait for the page to
be fully loaded (by checking readystate), and then get the
document object model (window.document). The reason for getting
the DOM is that it makes it easy to parse the html. Instead of
tabbing through the controls, you can pick up a reference to the
control you want by either getting the element from its id, or
by enumerating the elements (say, input elements or button
elements) and then picking out the one you want by its caption.

Finally, when you find the element you are looking for, then
you can use the click verb to trigger the submit action.

cheers, jw
____________________________________________________________

You got questions? WE GOT ANSWERS!!! ..(but, no guarantee
the answers will be applicable to the questions)
My System SpecsSystem Spec
Old 08-23-2008   #3 (permalink)
LuisE
Guest


 
 

Re: Using sendkeys in a web form

Thanks a lot for the reply. I hope I find you reliable, not unreliable.

I'm new to VBscript as you can see, I was exploring the elementsId but I
couldn't get anything out of it. I went to see the website's source trying
to find the elements IDs but not luck.

document.getElementByID("LoginButtonID").Click()

Should I do something like For each element ........

Could you please guide me towards a more specific reference so I can take it
from there??

Thanks again.

"mr_unreliable" wrote:
Quote:

> LuisE wrote:
Quote:

> > I need a script to open a website, tab thru a form in that webpage and
> > "submit" or "enter"at the 11th field (after doing tab for 11 times).
>
> hi LuisE,
>
> Instead of using sendkeys, you would be much better off to use
> the "document object model" of the page you are navigating to.
>
> There are a number of example scripts posted on this newsgroup,
> showing how to do that. In general, you wait for the page to
> be fully loaded (by checking readystate), and then get the
> document object model (window.document). The reason for getting
> the DOM is that it makes it easy to parse the html. Instead of
> tabbing through the controls, you can pick up a reference to the
> control you want by either getting the element from its id, or
> by enumerating the elements (say, input elements or button
> elements) and then picking out the one you want by its caption.
>
> Finally, when you find the element you are looking for, then
> you can use the click verb to trigger the submit action.
>
> cheers, jw
> ____________________________________________________________
>
> You got questions? WE GOT ANSWERS!!! ..(but, no guarantee
> the answers will be applicable to the questions)
>
My System SpecsSystem Spec
Old 08-23-2008   #4 (permalink)
James Whitlow
Guest


 
 

Re: Using sendkeys in a web form

"LuisE" <legonzales@xxxxxx> wrote in message
news:BBBD29C4-4E30-45A6-848E-726E2E697FFC@xxxxxx
Quote:

>I need a script to open a website, tab thru a form in that webpage and
> "submit" or "enter"at the 11th field (after doing tab for 11 times). I can
> get the website open and the window acticvate but in order to be able to
> tab
> inside the page I need to "click" on it. How can I replicate the "click"
> event by using SendKeys or other method of VBScript.
>
> Here is what I have
>
> Set wshshell=CreateObject("wscript.shell")
>
> With CreateObject("InternetExplorer.Application")
> .Navigate "http://www.hcso.tampa.fl.us/pub/default.asp?/Online/sname01"
> .visible=true
> End with
As mr_unreliable stated, use the methods of IE. Another method to
consider would be using the post URL. Below is a sample.

'~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Set oIE = CreateObject("InternetExplorer.Application")

sDate = "082208"
sPostURL = "http://www.hcso.tampa.fl.us/pub/default.asp?" _
& "/Online/dailybook3"

oIE.navigate sPostURL & "?Date=" & sDate & "&report=D"
oIE.visible = True
Do Until oIE.ReadyState = 4 : WScript.Sleep 100 : Loop
'~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~


My System SpecsSystem Spec
Old 08-23-2008   #5 (permalink)
LuisE
Guest


 
 

Re: Using sendkeys in a web form

Thanks James, it works fine.

In your code, what should I change if I need to use different fields of the
form?



"James Whitlow" wrote:
Quote:

> As mr_unreliable stated, use the methods of IE. Another method to
> consider would be using the post URL. Below is a sample.
>
> '~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
> Set oIE = CreateObject("InternetExplorer.Application")
>
> sDate = "082208"
> sPostURL = "http://www.hcso.tampa.fl.us/pub/default.asp?" _
> & "/Online/dailybook3"
>
> oIE.navigate sPostURL & "?Date=" & sDate & "&report=D"
> oIE.visible = True
> Do Until oIE.ReadyState = 4 : WScript.Sleep 100 : Loop
> '~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
>
>
>
My System SpecsSystem Spec
Old 08-23-2008   #6 (permalink)
James Whitlow
Guest


 
 

Re: Using sendkeys in a web form

"LuisE" <legonzales@xxxxxx> wrote in message
news:433ED65F-7746-4235-867A-BE58AF0C2338@xxxxxx
Quote:

> Thanks James, it works fine.
>
> In your code, what should I change if I need to use different fields of
> the
> form?
You will need to look at the source code of the page to determine that.
In the example you posted, I right clicked, selected 'View Source' and
searched for 'Booking Date:' & then 'Date:' under that. I searched up for
'action' to find the post URL. I then looked at the names of the input
fields. The date field is named 'Date' & the Report radio buttons are named
'report'. Just take the Post URL, add a question mark and then the field
names followed by an equal sign and the value you wish to pass. Separate the
fields with ampersands. In this case, I added '?Date=082208&report=D' to the
end of the Post URL.

I am not sure of a way to get the field names other than digging through
the HTML source code. There are probably others in the group that can offer
you a better way to do this. Firefox 2.x used to provide the fields names in
'View Page Info', but Firefox 3.x does not.

If you have trouble with the above, just advise the field names you are
looking for and either myself of someone else from the group can probably
help you determine them.


My System SpecsSystem Spec
Old 08-24-2008   #7 (permalink)
Al Dunbar
Guest


 
 

Re: Using sendkeys in a web form


"James Whitlow" <jwhitlow.60372693@xxxxxx> wrote in message
news:udhHHOWBJHA.2476@xxxxxx
Quote:

> "LuisE" <legonzales@xxxxxx> wrote in message
> news:433ED65F-7746-4235-867A-BE58AF0C2338@xxxxxx
Quote:

>> Thanks James, it works fine.
>>
>> In your code, what should I change if I need to use different fields of
>> the
>> form?
>
> You will need to look at the source code of the page to determine that.
> In the example you posted, I right clicked, selected 'View Source' and
> searched for 'Booking Date:' & then 'Date:' under that. I searched up for
> 'action' to find the post URL. I then looked at the names of the input
> fields. The date field is named 'Date' & the Report radio buttons are
> named 'report'. Just take the Post URL, add a question mark and then the
> field names followed by an equal sign and the value you wish to pass.
> Separate the fields with ampersands. In this case, I added
> '?Date=082208&report=D' to the end of the Post URL.
>
> I am not sure of a way to get the field names other than digging
> through the HTML source code. There are probably others in the group that
> can offer you a better way to do this. Firefox 2.x used to provide the
> fields names in 'View Page Info', but Firefox 3.x does not.
>
> If you have trouble with the above, just advise the field names you are
> looking for and either myself of someone else from the group can probably
> help you determine them.
Most web pages are designed to present the human user an interface with
visual clues that make it intuitively obvious how to interact with the page
using keyboard and mouse via common controls and navigation standards. You
could develop two pages that looked and operated exactly the same, but using
completely different HTML coding underneath, so I suspect that James'
statement that "You will need to look at the source code of the page" is
right on the money.

Not only that, but you might find that internal changes in the page you are
accessing might eventually break your external code.

I wonder if a standard will eventually arise (a document object model
approach, perhaps) that will make web pages more navigable by external
script. This could, of course, result in fewer ads being seen, so perhaps
there are forces working against this approach.

/Al


My System SpecsSystem Spec
Old 08-24-2008   #8 (permalink)
Bob Bridges
Guest


 
 

Re: Using sendkeys in a web form

A few comments:

1) I didn't realize you could just add the values of fields to a URL; I
thought I had to get my program to fill in the values of the text boxes,
drop-down lists etc, and then simulate a click on the correct button. I
should spend more time in this forum.

2) I've had occasion to automate access to the occasional web page that
didn't assign names to the fields I wanted, so getElementById wasn't
avaailable. (In fact, at least one made a point of assigning random names
with each new invocation; I suppose they wanted to prevent the black hats
from ruining their site with undesirable automated attacks. Thus I've
developed some methods of getting at some fields (once I've identified them)
by other means than ...ById or ...ByName. In fact I've had to do it often
enough that I've written a Class and methods for the purpose, though of
course I'm still tweaking it. If you've had to do this once, it's a
lead-pipe cinch you'll want to do it again and again; I think you may as well
make up your mind to write your own class to save you time.

3) One of the ways of getting at fields without a name or ID is to navigate
down the ChildNode tree. But finding an element in the tree, so that you'll
know where to navigate to, is MUCH harder than finding it in the raw HTML
code - more useful, sometime, but harder. I eventually surrendered to
inevitability and wrote a method for my class that maps an entire HTML
document tree - wrote it in VBA/Excel, that being my need at the moment, but
the concept shouldn't be hard to transfer to VBS and text output.

I mention all this partly to solicit comments and partly with an eye to
exchanging useful code relating to this problem.

--- Al Dunbar wrote:
Most web pages are designed to present the human user an interface with
visual clues that make it intuitively obvious how to interact with the page
using keyboard and mouse via common controls and navigation standards. You
could develop two pages that looked and operated exactly the same, but using
completely different HTML coding underneath, so I suspect that James'
statement that "You will need to look at the source code of the page" is
right on the money. Not only that, but you might find that internal changes
in the page you are accessing might eventually break your external code.

I wonder if a standard will eventually arise (a document object model
approach, perhaps) that will make web pages more navigable by external
script. This could, of course, result in fewer ads being seen, so perhaps
there are forces working against this approach.

--- "James Whitlow" <jwhitlow.60372693@xxxxxx> wrote:
Quote:

> You will need to look at the source code of the page to determine that.
> In the example you posted, I right clicked, selected 'View Source' and
> searched for 'Booking Date:' & then 'Date:' under that. I searched up for
> 'action' to find the post URL. I then looked at the names of the input
> fields.... The date field is named 'Date' & the Report radio buttons are
> named 'report'. Just take the Post URL, add a question mark and then the
> field names followed by an equal sign and the value you wish to pass.
> Separate the fields with ampersands. In this case, I added
> '?Date=082208&report=D' to the end of the Post URL.
>
> I am not sure of a way to get the field names other than digging
> through the HTML source code. There are probably others in the group that
> can offer you a better way to do this. Firefox 2.x used to provide the
> fields names in 'View Page Info', but Firefox 3.x does not.
>
> If you have trouble with the above, just advise the field names you are
> looking for and either myself of someone else from the group can probably
> help you determine them.
Quote:

> "LuisE" <legonzales@xxxxxx> wrote:
Quote:

>> ...what should I change if I need to use different fields of the form?
My System SpecsSystem Spec
Old 08-25-2008   #9 (permalink)
James Whitlow
Guest


 
 

Re: Using sendkeys in a web form

"Bob Bridges" <rhbridg.RemoveThisNode@xxxxxx> wrote in message
news:42A49137-AF00-4F2B-A699-2777B8B2DEE3@xxxxxx
Quote:

>A few comments:
>
> 1) I didn't realize you could just add the values of fields to a URL; I
> thought I had to get my program to fill in the values of the text boxes,
> drop-down lists etc, and then simulate a click on the correct button. I
> should spend more time in this forum.
Be aware of a few things when using this method. In the URL used in this
example, the Post URL was a fully qualified URL. In more cases than not,
this is not the case. It will be a relative path. For instance, the Google
groups advanced search URL is 'http://groups.google.com/advanced_search'.
The Post URL listed in the page is '/groups', so you would use the parent
URL plus the Post URL: 'http://groups.google.com/groups'. If you want a
search of the word 'vbscript', you could use
'http://groups.google.com/groups?as_q=vbscript'.

The second thing to be aware of is URL encoding. If you want to do the
same search as above, but search for multiple words, like 'vbscript xml',
you need encode the space. You could use something like
'http://groups.google.com/groups?as_q=vbscript%20xml'. If you are not
already aware of URL encoding, see this page:
http://www.permadi.com/tutorial/urlEncoding/

I use a function to just encode the reserved characters rather than the
entire URL. Below is the function I use. It is not extensively tested &
might be missing some characters, so use it with caution.

Function EncodeURL(ByVal sURL)
Dim i, sChar, sToEncode
sToEncode = "$&+,/:;=?@ <>#%{}|\^~[]`"
For i = 1 to Len(sURL)
sChar = Mid(sURL, i, 1)
If InStr(sToEncode, sChar) > 0 Then sChar = "%" & Right("00" &
Hex(Asc(sChar)), 2)
EncodeURL = EncodeURL & sChar
Next
End Function


My System SpecsSystem Spec
Old 08-25-2008   #10 (permalink)
Al Dunbar
Guest


 
 

Re: Using sendkeys in a web form


"Bob Bridges" <rhbridg.RemoveThisNode@xxxxxx> wrote in message
news:42A49137-AF00-4F2B-A699-2777B8B2DEE3@xxxxxx
Quote:

>A few comments:
>
> 1) I didn't realize you could just add the values of fields to a URL; I
> thought I had to get my program to fill in the values of the text boxes,
> drop-down lists etc, and then simulate a click on the correct button. I
> should spend more time in this forum.
Adding field values to a URL is what the browser does in response to the
interactions you have with the mouse and the keyboard. Here, for example, is
the URL generated by a simple GOOGLE search for "look at this":

http://www.google.ca/search?hl=en&q=...e+Search&meta=

You could simply type in those calculated URL's, but it is generally easier
to interact with the GUI interface - unless you are a script.
Quote:

> 2) I've had occasion to automate access to the occasional web page that
> didn't assign names to the fields I wanted, so getElementById wasn't
> avaailable. (In fact, at least one made a point of assigning random names
> with each new invocation; I suppose they wanted to prevent the black hats
> from ruining their site with undesirable automated attacks.
They also want to increase the likelihood that visitors will see (and click
on) ads they have on their page that help pay their bills. Writing a script
to interact with a page is most likely *not* going to click on these links
(although you could consider attempting to add that "feature" to your code).
Quote:

> Thus I've
> developed some methods of getting at some fields (once I've identified
> them)
> by other means than ...ById or ...ByName. In fact I've had to do it often
> enough that I've written a Class and methods for the purpose, though of
> course I'm still tweaking it. If you've had to do this once, it's a
> lead-pipe cinch you'll want to do it again and again; I think you may as
> well
> make up your mind to write your own class to save you time.
Most probably.
Quote:

> 3) One of the ways of getting at fields without a name or ID is to
> navigate
> down the ChildNode tree. But finding an element in the tree, so that
> you'll
> know where to navigate to, is MUCH harder than finding it in the raw HTML
> code - more useful, sometime, but harder. I eventually surrendered to
> inevitability and wrote a method for my class that maps an entire HTML
> document tree - wrote it in VBA/Excel, that being my need at the moment,
> but
> the concept shouldn't be hard to transfer to VBS and text output.
I hinted at the possibility of developing a script-friendly web page DOM,
but I now think it more likely for web page developers to obfuscate the
situation by making it harder to navigate programmatically. Or perhaps some
might publish code for others such as yourself to use that presses the
buttons of their sponsours. Then again, the sponsours might not like to find
out that their buttons are being pushed by scripts that are not likely to
buy their products...
Quote:

> I mention all this partly to solicit comments and partly with an eye to
> exchanging useful code relating to this problem.
Definitely a good idea.

/Al
Quote:

> --- Al Dunbar wrote:
> Most web pages are designed to present the human user an interface with
> visual clues that make it intuitively obvious how to interact with the
> page
> using keyboard and mouse via common controls and navigation standards. You
> could develop two pages that looked and operated exactly the same, but
> using
> completely different HTML coding underneath, so I suspect that James'
> statement that "You will need to look at the source code of the page" is
> right on the money. Not only that, but you might find that internal
> changes
> in the page you are accessing might eventually break your external code.
>
> I wonder if a standard will eventually arise (a document object model
> approach, perhaps) that will make web pages more navigable by external
> script. This could, of course, result in fewer ads being seen, so perhaps
> there are forces working against this approach.
>
> --- "James Whitlow" <jwhitlow.60372693@xxxxxx> wrote:
Quote:

>> You will need to look at the source code of the page to determine
>> that.
>> In the example you posted, I right clicked, selected 'View Source' and
>> searched for 'Booking Date:' & then 'Date:' under that. I searched up for
>> 'action' to find the post URL. I then looked at the names of the input
>> fields.... The date field is named 'Date' & the Report radio buttons are
>> named 'report'. Just take the Post URL, add a question mark and then the
>> field names followed by an equal sign and the value you wish to pass.
>> Separate the fields with ampersands. In this case, I added
>> '?Date=082208&report=D' to the end of the Post URL.
>>
>> I am not sure of a way to get the field names other than digging
>> through the HTML source code. There are probably others in the group that
>> can offer you a better way to do this. Firefox 2.x used to provide the
>> fields names in 'View Page Info', but Firefox 3.x does not.
>>
>> If you have trouble with the above, just advise the field names you
>> are
>> looking for and either myself of someone else from the group can probably
>> help you determine them.
>
Quote:

>> "LuisE" <legonzales@xxxxxx> wrote:
Quote:

>>> ...what should I change if I need to use different fields of the form?

My System SpecsSystem Spec
Reply

Thread Tools


Similar Threads
Thread Forum
Strange Sendkeys() problem PowerShell
Sending Alt-Space with SendKeys in C# .NET General
sendkeys, ie7, forms, any new bugs? VB Script
SendKeys to cmd.exe PowerShell
Sendkeys problem with Vista Vista General


Vista Forums is an independent web site and has not been authorized,
sponsored, or otherwise approved by Microsoft Corporation.
"Windows Vista", the Start Orb, and related materials are trademarks of Microsoft Corp.
© Designer Media Ltd

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46