views:

157

answers:

2

hi;

i need grab to internet explorer address bar. how to get address bar url for python ? (i need second part other browsers grabbing address bar but internet explorer is urgently).

Thanks.

+1  A: 

Would something like this not work?

import win32gui

def get_current_window_title():
   cur_hwnd = win32gui.GetForegroundWindow()
   win_title = win32gui.GetWindowText(cur_hwnd)
   return win_title
rlotun
thanks man for answer. but i'm need address bar. not need title.
john misoskian
+1  A: 

The following works for me.

from win32com.client import Dispatch

SHELL = Dispatch("Shell.Application")

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

def main():
    ie = get_ie(SHELL)
    if ie:
        print ie.LocationURL
    else:
        print "no ie window"

if __name__ == '__main__':
    main()

I tried to use Dispatch('InternetExplorer.Application') to connect to current IE window, but it would always create a new window. The code would have been simpler.

# This doesn't work
from win32com.client import Dispatch

# This line will always create a new window
ie = Dispatch("InternetExplorer.Application")

print ie.LocationURL
PreludeAndFugue