win32com: makepy causes ie problems.

brueckd at tbye.com brueckd at tbye.com
Thu Jul 18 23:41:41 EDT 2002


On Thu, 18 Jul 2002 brueckd at tbye.com wrote:

> I use a function like this:
> 
> def WaitForDoc(doc, extraDelay=0.0):
>   if extraDelay:
>     time.sleep(extraDelay)
>   while not (hasattr(doc, 'readyState') and doc.readyState == 'complete'):
>     time.sleep(0.1)

I realized I might need to add a little more code to give this better 
context; here's some additional functions I use:

def Navigate(ie, url):
    ie.Navigate(url)
    while ie.Busy: time.sleep(0.05)
    doc = ie.Document
    WaitForDoc(doc)
    return doc

def HasLink(obj, pattern):
    'Returns matching link or None. obj does not have to be a document'
    pattern = pattern.lower()
    try:
        links = obj.Links
    except AttributeError:
        links = obj.all.tags('a') # Slower, but works for non-doc objs
    for i in range(links.length):
        link = links[i]
        if fnmatch.fnmatch(link.href.lower(), pattern):
            return link
    return None

def ClickLink(obj, pattern, delay=0):
    'Clicks on the first link that matches the text pattern. obj can be 
non-document object'
    link = HasLink(obj, pattern)
    assert link, 'No such link in document'
    link.Click()
    try:
        doc = obj.document
    except AttributeError:
        doc = obj
    WaitForDoc(doc, delay)

def CreateInstance(isVisible):
    ie = win32com.client.DispatchEx('InternetExplorer.Application.1')
    doc = Navigate(ie, 'about:blank')
    ie.Visible = isVisible
    if isVisible:
        # Hack alert! Make the window come to the foreground
        try:
            hwnd = win32gui.FindWindow(None, 'about:blank - Microsoft 
Internet Explorer')
            if hwnd:
                win32gui.SetForegroundWindow(hwnd)
        except pywintypes.error, e:
            print e
        
    return ie

Sample usage:
ie = CreateInstance(1)
doc = Navigate(ie, 'http://www.google.com')
doc.f.q.Value = 'python'
doc.f.Submit()
WaitForDoc(doc)

-Dave






More information about the Python-list mailing list