[python-win32] How to get URL of open browser window?

Tim Golden mail at timgolden.me.uk
Sun Nov 18 16:27:11 CET 2007


Robert wrote:
> What is the best way to get the URL of current open webbrowser 
> Window (topmost) into a utility script? At least for IE & Mozilla's

I don't think that there is a straightforward answer to this
question. Assuming you're starting from nothing, there are
two issues here: find an open web browser; find out what
page that web browser is on.

Even if you limit yourself to IE & Firefox (and there are
others) you still have to do the "Find a top-level window"
dance. What do you do if there is more than one such
open? Or if there is one of each?

Once you've done that -- say, by choosing the first one
you come to -- you then have to adopt multiple strategies
for determining the current URL. And the first question is:
What is the "current" URL for a browser with multiple tabs.

I don't have one to hand, but you examples abound on the
Web of finding the top-level window for an application.
Modules like pywinauto [1] or WATSUP [2] can probably
help you out.

Once you've found the window, with IE you should be able
to interrogate it via COM. Have a look at PAMIE [3] but
I don't think Firefox is COM-scriptable you might have
to fallback on pywinauto to interrogate windows class
by class to spot the one with the URL in it.

TJG

[1] http://www.openqa.org/pywinauto/
[2] http://www.tizmoi.net/watsup/intro.html
[3] http://pamie.sourceforge.net/




More information about the python-win32 mailing list