Get IHTMLDocument2 for UI Automation in Python

Some software embed ie control in win32 window as the User Interface, such as Internet Explorer_Server. We can hardly identify the html control inner Internet Explorer_Server through Spy++. So it takes a great pain to ui automation. This post, I will introduce a way for Internet Explorer_Server automation.

Get IHTMLDocument2 interface

Also see:

Here is my python implentment:

#!/usr/bin/env python

__author__ = 'CoderZh'

import sys

# Important for multithreading
sys.coinit_flags = 0 # pythoncom.COINIT_MULTITHREADED

import win32com
import win32com.client
import win32gui
import win32con
import pythoncom

def getIEServer(hwnd, ieServer):
if win32gui.GetClassName(hwnd) == 'Internet Explorer_Server':

if __name__ == '__main__':
#pythoncom.CoInitializeEx(0) # not use this for multithreading
    mainHwnd = win32gui.FindWindow('windowclass''windowtitle')
if mainHwnd:
= []
        win32gui.EnumChildWindows(mainHwnd, getIEServer, ieServers)
if len(ieServers) > 0:
= ieServers[0]
= win32gui.RegisterWindowMessage('WM_HTML_GETOBJECT')
            ret, result 
= win32gui.SendMessageTimeout(ieServer, msg, 0, 0, win32con.SMTO_ABORTIFHUNG, 1000)
= pythoncom.ObjectFromLresult(result, pythoncom.IID_IDispatch, 0)
= win32com.client.dynamic.Dispatch(ob)

print doc.url



IHTMLDocument2 is thread safe, we can not use it in any other thread except the main thread by default. If you want to use it in multi threads, write these code at the top of your file, and remenber do not explicit call pythoncom.CoInitializeEx(0) and pythoncom.CoUninitialize() :
# Important for multithreading
sys.coinit_flags = 0 # pythoncom.COINIT_MULTITHREADED

Also see:


IHTMLDocument2 for UI Automation

Also see :

Now, you can write your own UI Automation framework by IHTMLDocument2.

posted @ 2010-05-05 01:23  CoderZh  阅读(2045)  评论(0编辑  收藏  举报