|
IEC.py - Automating Internet Explorer with Python
IEC is a python library designed to help you automate and control an Internet Explorer window.
You can use this library to navigate to web pages, read the values of various HTML elements,
set the values of checkboxes, text boxes, radio buttons etc., click on buttons and submit forms.
Typical uses for this library include:
- Writing test scripts to check your web application automatically.
- Managing an online account automatically.
- Automatically logging and downloading webmail.
- Monitoring web based applications periodically for failure.
Since this library uses Internet Explorer to do its processing, it has capabilities
that a lot of other web libraries lack. Some of the features it has over httplib, urllib and
other such python web libraries are:
- Ability to handle page redirects (javascript, HTTP and HTML).
- Ability to process Javascript and VBscript.
- Easily handles HTML form elements.
- Handles multiple protocols such as http://, https://, rtsp://, ftp:// etc. and can also
run these protocols on non-standard ports.
- Automatic cookie handling and management.
This program is an useful addition to any developer's toolkit and
best of all, it is released under a BSD-style license,
which means you are free to modify it as needed.
If you find this program useful, or have any suggestions for
improvements, please e-mail me and let me know. Maybe you will see
your name in the credits section :). Also, please consider
linking to this page, if you find this program useful.
|
You can download the program by clicking
on this link. You'll need WinZIP or some similar program
to decompress the code. There are two files in the archive:
- IEC.py - Contains the library code.
- README - Contains the documentation.
|
First, you should have a fairly basic knowledge of Python. It would also help
if you understand HTML coding standards, especially how HTML form elements work.
You can easily find several HTML references on the web.
The program uses Internet Explorer's COM object interface to manipulate it.
Hence, it only works on Microsoft Windows systems.
IEC is implemented in python and therefore requires a python
interpreter to be installed in the system. The library has been tested with
python versions from 2.2 to 2.4. You can get a python interpreter from
http://www.python.org/
To allow Python to work with COM interfaces, you will need Mark Hammond's excellent set of
Python for Windows Extensions,
in particular his win32com module. Note that some users may need to download
MFC71.DLL and
place it in their C:\WINDOWS\SYSTEM32 directory, in case Mark's installer
starts complaining about missing DLLs.
|
An issue with using COM objects in Python, is the matter of Late Binding vs. Early Binding.
Basically, there are two ways that a
Python COM object can access its methods and properties. These two methods are called Late Binding and
Early Binding. If a Python COM object uses Late Binding, then every time you access a
method or property of the object, it goes through the IDispatch interface to find the method/property,
even if it is the same one being called each time. With Early Binding, we let Python know ahead
of time, what methods and properties are available to an object. This speeds up things significantly,
especially inside loops, and the performance gains are actually quite substantial.
To enable Early Binding for the Internet Explorer object, we need to import the Internet Explorer library.
To do this:
- In PythonWin, go to Tools --> COM Makepy Utility
- In the dialog box that pops up, scroll down till you reach Microsoft Internet Controls.
If there are multiple versions, simply pick the latest one.
- Click on the OK button. The PythonWin environment will freeze for a little bit, while
the library is being imported. In a little while, you should see a message on the Interactive
Window that says that a file was generated.
- You've successfully generated a Python type library for Internet Explorer. From now on, Python will automatically
use the type library to early-bind any Internet Explorer objects.
You can get by without importing the Internet Explorer library, but the performance gains are well worth it.
|
Using the library to control IE is as simple as:
import IEC
ie = IEC.IEController() # Create a new IE Window.
ie.Navigate('http://www.google.com/') # Navigate to a website.
ie.SetInputValue('q', 'mayukh bose') # Fill in the search box.
ie.ClickButton(caption='Google Search') # Click on the search button.
The library has its own demo built into it. You can open IEC.py inside PythonWin and run the
class file by itself, to activate the built in demo. Note that the demo program displays message boxes
that may appear behind the current IE window. Hence, if the IE program pauses, switch to the python
program and click on the message box's OK button to continue.
There is also a README file included, which contains documentation about all the methods implemented
by the IEController object.
|
My family for always being there for me, and Suzanne Young, who I love to pieces.
sfb for suggesting a few changes to my string code. The devshed crew for their feedback.
David L. Wooden (davidwooden at comcast dot net) for suggesting GetCurrentURL() and improving PollWhileBusy().
Finally, a big thank you to you, the user. Remember, if it wasn't
for you, this program may have never been useful in the first place.
|
|