473,651 Members | 3,024 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Automatically populate and submit HTML Forms

rbt
How can I make a python client script connect to a Web server and
automatically populate form fields and then submit the form?

For example, say I wanted to check and see if 1924 was a leap year...
how would I populate the 'year' field and then submit it for processing?
I have no control of the server... just the client script.

<form action="cgi-bin/leapyear.py">
<p>Enter a year and find out if it's a leap year:
<input type="text" name="year" size="6">
<input type="submit">
<input type="reset">
</form>

Many thanks,

rbt
Jul 19 '05 #1
5 9072
Hey rbt,

You should take a look at mechanize:
http://wwwsearch.sourceforge.net/mechanize/

One of its listed features is 'Easy HTML form filling, using ClientForm
interface'.

A more recent option which came up on this group a few days ago is
twill: http://www.idyll.org/~t/www-tools/twill.html

twill combines pyparsing & mechanize to provide a way of scripting
tests for web applications. While it might not offer the functionality
you're after (I haven't had call to use either of these), it looks very
accessible, and could provide some good examples for using mechanize
with your own scripts.

-alex23

Jul 19 '05 #2
These two blog entries might be of help to you.

http://www.ishpeck.net/index.php?P=b1115239318ishpeck
second half is at
http://www.ishpeck.net/index.php?P=b1115225809ishpeck

alex23 wrote:
Hey rbt,

You should take a look at mechanize:
http://wwwsearch.sourceforge.net/mechanize/

One of its listed features is 'Easy HTML form filling, using ClientForm
interface'.

A more recent option which came up on this group a few days ago is
twill: http://www.idyll.org/~t/www-tools/twill.html

twill combines pyparsing & mechanize to provide a way of scripting
tests for web applications. While it might not offer the functionality
you're after (I haven't had call to use either of these), it looks very
accessible, and could provide some good examples for using mechanize
with your own scripts.

-alex23


Jul 19 '05 #3
On 2005-05-26, Grant Edwards <gr****@visi.co m> wrote:
How can I make a python client script connect to a Web server and
automatically populate form fields and then submit the form?
Just use urllib() and pass the form data to the urlopen()
method. If given data, it will generate a "POST" request
instead of a "GET".


Oops, I forgot. Here's a pointer to the examples in the urllib
docs:

http://docs.python.org/lib/node478.html

--
Grant Edwards grante Yow! This is PLEASANT!
at
visi.com
Jul 19 '05 #4
On 2005-05-25, rbt <rb*@athop1.ath .vt.edu> wrote:
How can I make a python client script connect to a Web server and
automatically populate form fields and then submit the form?

For example, say I wanted to check and see if 1924 was a leap year...
how would I populate the 'year' field and then submit it for processing?
I have no control of the server... just the client script.

<form action="cgi-bin/leapyear.py">
<p>Enter a year and find out if it's a leap year:
<input type="text" name="year" size="6">
<input type="submit">
<input type="reset">
</form>


Just use urllib() and pass the form data to the urlopen()
method. If given data, it will generate a "POST" request
instead of a "GET". Here's a snippet of code from an app of
mine that "fills in a form" and submits it:

postData = urllib.urlencod e({'submit':'Re move','disp':'M ','action':'cha nge_Msgs'})

for msgid in msgIDs:
postData += "&msgid="+m sgid

req2 = urllib2.Request ("http://mc-s6.postini.com/exec/MsgCtr",postDat a)
rsp2 = ClientCookie.ur lopen(req2)

In this code I've eyeballed the form and the field names are
hard-wired into the code. If your form doesn't change from one
usage to the next, that's the simplest way to do it.

In my example I'm using ClientCookie and urllib2 to create/open
the reqeust in two steps because the request seen above won't
work without some cookie values previsouly established in code
that I've snipped. Otherwise all you'd need to do is something
like this:

urllib.urlopen( 'http://whatever',
urllib.urlencod e({'field1Name' :'value1','fiel d2Name':'value2 '}))

--
Grant Edwards grante Yow! Yes, but will I
at see the EASTER BUNNY in
visi.com skintight leather at an
IRON MAIDEN concert?
Jul 19 '05 #5
rbt
Grant Edwards wrote:
On 2005-05-25, rbt <rb*@athop1.ath .vt.edu> wrote:
How can I make a python client script connect to a Web server and
automatical ly populate form fields and then submit the form?

For example, say I wanted to check and see if 1924 was a leap year...
how would I populate the 'year' field and then submit it for processing?
I have no control of the server... just the client script.

<form action="cgi-bin/leapyear.py">
<p>Enter a year and find out if it's a leap year:
<input type="text" name="year" size="6">
<input type="submit">
<input type="reset">
</form>

Just use urllib() and pass the form data to the urlopen()
method. If given data, it will generate a "POST" request
instead of a "GET". Here's a snippet of code from an app of
mine that "fills in a form" and submits it:

postData = urllib.urlencod e({'submit':'Re move','disp':'M ','action':'cha nge_Msgs'})

for msgid in msgIDs:
postData += "&msgid="+m sgid

req2 = urllib2.Request ("http://mc-s6.postini.com/exec/MsgCtr",postDat a)
rsp2 = ClientCookie.ur lopen(req2)

In this code I've eyeballed the form and the field names are
hard-wired into the code. If your form doesn't change from one
usage to the next, that's the simplest way to do it.

In my example I'm using ClientCookie and urllib2 to create/open
the reqeust in two steps because the request seen above won't
work without some cookie values previsouly established in code
that I've snipped. Otherwise all you'd need to do is something
like this:

urllib.urlopen( 'http://whatever',
urllib.urlencod e({'field1Name' :'value1','fiel d2Name':'value2 '}))


Thanks Grant... I found ClientCookie and ClientForm here:

http://wwwsearch.sourceforge.net/

Two great Python modules that really simplify this!!!
Jul 19 '05 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
2349
by: Phillip Windell | last post by:
This is really an HTML question I think, but it probably won't take much effort to answer (unless I'm answering or course). The HTML is all ASP generated and the ASP generates exactly what I want but the resulting HTML isn't working right. It consists of multiple forms on a page with each form using a unique name. The submit button in each form also uses a unique name. The forms are supposed to submit a single value from a hidden field...
4
7786
by: Sarah | last post by:
Hi all. I have a form, and several text and image links on it that should submit the form with different actions. I prepared a simple page with just the code that's not working. PROBLEM: The form won't submit if the link is clicked, but will submit if the SUBMIT button is clicked. I need to call a function to change the form's action according to user's input before it is submitted.
20
12381
by: Dannyboyo | last post by:
I have what I hope is a simple request. I can't really code in javascript, but I am pretty good at cusomizing it with slight modifications. I code in ASP and HTML. I am trying to capture customer input of product names to put on custom labels we make. Some of the labels will have our product names on them, but the customer can add other products that we do not sell. So, on my product detail page I want a textbox that can have rows copied...
0
747
by: sophocles the wise | last post by:
automatically post data & click buttons on sequence of web pages with AxWebBrowser Hi, I need to post stuff everyday to a website and am working on a VB program to do this for me. I am a computer technician and I post an ad on craigslist.org everyday but sometimes I don't have time to do this. So, I accomplished so far to load the first page and post my ad in the textbox, with title and email address, and to alter the radio button and...
4
4991
by: Rizyak | last post by:
This message is cross posted in alt.comp.lang.php & comp.lang.javascript I have a form for a user to input an establishment's hours and what time an event is taking place. After the user inputs their establishment's hours of operation I want the form elements lower in the form to adjust so that an event can only happen when the place is open. I have two fields for the hours: These are both select fields with values between 0-23
1
2827
by: got4a | last post by:
Hi guys, Lots of threads and ideas, but I couldn't find what I'm looking for. Hence, this. I have asp page (say, 1.asp) that has a textbox. This page is available to me in read-only mode, meaning I know all the field names, functions names etc., but can't change anything. Now, I need to write .htm or .asp (2.htm or 2.asp) that will have a textbox and submit button. When user clicks on submit two things have
1
6501
by: vj | last post by:
How i can populate all fileds dynamically in jsp page based on contents found in xml file? I have written jsp servlets and java class file. i transferred automatic data from jsp to servlet then to java class which creates a xml file based on values entered in dynamic jsp page. Now i want to read all those values entered to xml in my other jsp page. I am able to call values from file in my jsp page. But as dynamic values can be any in no...
0
8278
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8807
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
8466
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8584
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
7299
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
5615
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
1
2701
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
1
1912
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
2
1588
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.