Comfortable Internet Explorer Automation

Do you want to automate Microsoft Internet Explorer more comfortable than ever with Python ? Then maybe PAMIE is helpful for you. PAMIE (Python Automation Module For Internet Explorer) is a free opensource Python win32com script for automation of Internet Explorer – Mainly designed for QA engineers with scripting ability.

>You can download PAMIE from http://sourceforge.net/projects/pamie

Here are some functionalities listed:

_frameWait - waits for user speficied frame document to load
_wait - wait for documetn to load
checkForFormName - check to see if a formname exists in the document
checkForFrameName -- check to see if a framename exists
ClickBtnImage - Clicks on an input type image button
ClickButton - Click on a button
ClickImage - clicks on an Image
ClickLink - Clicks on a link
ClickMenu - Clicks on an menu item,link
ClickMenuImage - Clicks on an menu item that is a image.
Click a Menu or Sub Menu Item
ClickNthImage click on the Nth Image
ClickNthLink - click on the Nth link
ClickOuterLink - clicks  on a link using outertext
ClickPageLink - click on the page link
ClickIndexLink
printLinkIndex
GetTextBox - get tectbox value
SettextBox  - Sets texbox value
GetNames - get the names of the form elements
GetListbox - gets selected listbox item
SetlistBox - sets selected listbox item
GetRadioButton - Gets Radio button status
SetRadioButton  - Sets Radio button
GetRadioButtonSet - same as above
getTableData - returns the text in table
showAllTableText - finds all text(indexed)in table on the web page
showTableLinkText - finds the link text
dialogcontroller - manipulates modal dialogs
ExecuteJS - executes a JavaScript Function on the page

and much much more !!!

Grab text or source from HTML pages

import win32com.client
from time import sleep

def download_url_with_ie(url):
    """
    Given a url, it starts IE, loads the page, gets the HTML.
    Works only in Win32 with Python Win32com extensions enabled.
    Needs IE. Why? If you’re forced to work with Brain-dead 
    closed sourceapplications that go to tremendous length to deliver
    output specific to browsers; and the application has no interface
    other than a browser; and you want get data into a CSV or XML
    for further analysis;
    Note: IE internally formats all HTML to stupid mixed-case, no-
    quotes-around-attributes syntax. So if you are planning to parse
    the data, make sure you study the output of this function rather
    than looking at View-source alone.
    """

    #if you are calling this function in a loop, it is more
    #efficient to open ie once at the beginning, outside this
    #function and then use the same instance to go to url’s
    ie = win32com.client.Dispatch("InternetExplorer.Application")

    ie.Visible = 1 #make this 0, if you want to hide IE window
    #IE started
    ie.Navigate(url)
    #it takes a little while for page to load. sometimes takes 5 sec.
    if ie.Busy:
        sleep(5)
    #now, we got the page loaded and DOM is filled up
    #so get the text
    text = ie.Document.body.innerHTML
    #text is in unicode, so get it into a string
    text = unicode(text)
    text = text.encode('ascii','ignore')
    #save some memory by quitting IE! **very important** 
    ie.Quit()
    #return text
    print text
download_url_with_ie('http://www.goermezer.de')

Get all URLs from a webpage

This example shows how to automate Microsoft Internet Explorer with Python/Pywin32.

It
opens a Internet Explorer window, surfs to www.goermezer.de (and waits 3
sec to load the page), and prints all links of the loaded page.

Some links to the Object Model of Microsoft Internet Explorer:

Microsoft ActiveX control

DHTML Objects 

IHTMLDocument2 Interface

import win32com.client, time
ie = win32com.client.Dispatch("InternetExplorer.Application") 
ie.Visible = 1 
ie.Navigate('http://www.goermezer.de') 
time.sleep(3) #wait 3 sec. 
print 'You are surfing on', ie.Document.domain 
print 'And now a list of the Links:' 
for i in ie.Document.links: 
    print i

Executing Javascript function in a web page in IE

The following code executes a Javascript function on a web page using IE and COM.

import win32com.client, pythoncom
from time import sleep

ie=win32com.client.Dispatch('internetexplorer.application')
ie.Visible=1
ie.Navigate('http://www.cpplab.com/Articles/JSCalls/TestPage/JSCallTestPage.htm')
sleep(5) # Give it time to get there.

id=ie.Document.Script._oleobj_.GetIDsOfNames('Multiply') # 'Multiply' is the function name.
#5 and 2 below are the parameters passed to the function 'Multiply'.
res=ie.Document.Script._oleobj_.Invoke(id, 0, pythoncom.DISPATCH_METHOD, True, 5, 2)

Creating multiple instances of IE COM browser objects.

The following code allows you to create more than one IE browser session and interact with each different one.

# Use the makepy utility for the "Microsoft Internet Controls (1.1)"

# object library to get early binding.
from win32com.client import Dispatch
import pythoncom

ie1 = pythoncom.CoCreateInstance("InternetExplorer.Application", None,\
pythoncom.CLSCTX_SERVER,
pythoncom.IID_IDispatch)
ie1 = Dispatch(ie1)

ie2 = pythoncom.CoCreateInstance("InternetExplorer.Application", None,\
pythoncom.CLSCTX_SERVER,
pythoncom.IID_IDispatch)
ie2 = Dispatch(ie2)

# and so on.....

Printing Webpages from Internet Explorer with Python

This script calls the webpage www.heise.de in Internet Explorer and prints it to the standard printer.

Attention: use the makepy.py utility (C:\\Python23\\Lib\\site-packages\\win32com\\client\\) on the “Microsoft Internet Controls (1.1)” object library to get early binding !!! Otherwise you cannot print !

# print a webpage in Internet Explorer (COM Example)
# Attention: use the makepy utility for the "Microsoft Internet Controls (1.1)"
# object library to get early binding !!!

from win32com.client import Dispatch
from time import sleep

ie = Dispatch(“InternetExplorer.Application”)
ie.Visible = 1
ie.Navigate(“http://www.heise.de”)
if ie.Busy:
sleep(2)
# print the current IE document without prompting the user for the printerdialog
ie.ExecWB(win32com.client.constants.OLECMDID_PRINT, \
win32com.client.constants.OLECMDEXECOPT_DONTPROMPTUSER)