Search code examples
pythoninternet-explorerpywin32

how to get internet explorer address bar for python


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.


Solution

  • 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