On 1:59 PM, He Jibo wrote:
Hi,
I am writing a small program, which needs to get the URL of the active
tab in either of firefox, internet exploerer or chrome.
My need is similar as the one posted at,
http://stackoverflow.com/questions/3631216/how-do-i-get-the-url-of-the-visible-tab-in-firefox-ie-chrome <http://stackoverflow.com/questions/3631216/how-do-i-get-the-url-of-the-visible-tab-in-firefox-ie-chrome>

I did a lot of Googling, and get the following code. The following
code can get the url of the first tab in internet explorer. My
question is, how can I get the url of the current active tab? Thanks.

'''
http://efreedom.com/Question/1-2555905/Get-Internet-Explorer-Address-Bar-Python
http://blogs.msdn.com/b/oldnewthing/archive/2005/07/05/435657.aspx
http://mail.python.org/pipermail/python-win32/2004-June/002040.html
http://code.activestate.com/recipes/302324-browser-automation-tool-py-class-file/
'''
from win32com.client import Dispatch
import win32api, win32con,win32gui


SHELL = Dispatch("Shell.Application")

def get_ie(shell):
   for win in shell.Windows():
#        print win
       if win.Name == "Windows Internet Explorer":
           return win
   return None

def main():
ie = get_ie(SHELL)
   if ie:
       print ie.LocationURL
       print ie.LocationName
       print ie.ReadyState
       print ie
       print ie.Document.title
       print ie.Document.location
       print ie.Document.forms

#        title = win32gui.GetWindowText(ie)
#        print title

   else:
       print "no ie window"

if __name__ == '__main__':
   main()
---------------------------
He Jibo
Department of Psychology,
Beckman Institute for Advanced Science and Technology
University of Illinois, Urbana Champaign,
603 East Daniel St.,
Champaign, IL 61820
website: www.hejibo.info <http://www.hejibo.info>


It will probably be frowned on here, but one of the easiest ways would be to use SendKeys in combination with some Windows hackery. I have attached some code I came up with after getting help from several of the fine people on this list. It shows how to bring a window into focus by just passing in an expected string. In this case, you would want to use something like "Windows Internet Explorer" and have it search for that.

Once that's in focus, you can use the SendKeys package (http://www.rutherfurd.net/python/sendkeys/) to send an ALT+D to select the url and then CTRL+C to copy it. Then you can use the win32clipboard module from PyWin32 to grab the text from the clipboard.

It's a hack, but it's kind of fun. Of course, you can't use your PC while the script runs or you might interrupt the process and send the keys to the wrong window.


--
Mike Driscoll
Applications Specialist
Blog:   http://blog.pythonlibrary.org
import win32gui


def windowEnumerationHandler(hwnd, resultList):
    '''
    This is a handler to be passed to win32gui.EnumWindows() to generate
    a list of (window handle, window text) tuples.
    '''

    resultList.append((hwnd, win32gui.GetWindowText(hwnd)))

def bringToFront(windowText):
    '''
    Method to look for an open window that has a title that
    matches the passed in text. If found, it will proceed to
    attempt to make that window the Foreground Window.
    '''
    secondsPassed = 0
    while secondsPassed <= 5:
        # sleep one second to give the window time to appear
        wx.Sleep(1)

        print 'bringing to front'
        topWindows = []
        # pass in an empty list to be filled
        # somehow this call returns the list with the same variable name
        win32gui.EnumWindows(windowEnumerationHandler, topWindows)
        print len(topWindows)
        # loop through windows and find the one we want
        for i in topWindows:
            if windowText in i[1]:
                print i[1]
                win32gui.ShowWindow(i[0],5)
                win32gui.SetForegroundWindow(i[0])
        # loop for 5-10 seconds, then break or raise
        handle = win32gui.GetForegroundWindow()
        if windowText in win32gui.GetWindowText(handle):
            break
        else:
            # increment counter and loop again                
            secondsPassed += 1
_______________________________________________
python-win32 mailing list
python-win32@python.org
http://mail.python.org/mailman/listinfo/python-win32

Reply via email to