[Tutor] Selecting a browser
mwalsh at groktech.org
Tue Dec 4 02:25:14 CET 2007
Ricardo Aráoz wrote:
> Martin Walsh wrote:
> Hi Marty, thanks for your help.
> I've tried your suggestions but they don't seem to work for me. In W's
> system window I can do :
> C:/> S:\FirefoxPortable\FirefoxPortable.exe http://www.google.com
> and it will open my browser ok. But no matter what I try :
> "c:/program files/mozilla firefox/firefox.exe %s &" or "c:/program
> files/mozilla firefox/firefox.exe %s" as input to webbrowser.get() it
> won't work.
Never would have guessed that you were using a portable browser :) But
it really shouldn't matter. And by the way, the '&' has special meaning
to the webbrowser.get method -- it determines whether a
BackgroundBrowser or GenericBrowser object is returned.
> Here's my session :
>>>> import webbrowser
>>>> ff = webbrowser.get("S:\FirefoxPortable\FirefoxPortable.exe %s &")
I suspect (with no way to confirm at the moment) that something in the
webbrowser module is confused by the backslashes. As you may know, the
backslash has special meaning in python strings, used as an escape
character to denote newlines (\n), tabs (\t), among others. I believe it
is helpful to be aware of this when using subprocess.Popen also. And, I
think you have more than one option for dealing with slashes in windows
paths, but I typically just replace the backslashes with forward slashes:
ff = webbrowser.get("S:/FirefoxPortable/FirefoxPortable.exe %s &")
> Besides looking at the Python 2.5 documentation gave me no clues. It is
> very poorly documented e.g. :
> register( name, constructor[, instance])
> Register the browser type name. Once a browser type is registered, the
> get() function can return a controller for that browser type. If
> instance is not provided, or is None, constructor will be called without
> parameters to create an instance when needed. If instance is provided,
> constructor will never be called, and may be None.
> This entry point is only useful if you plan to either set the BROWSER
> variable or call get with a nonempty argument matching the name of a
> handler you declare.
> But it does not define what a 'constructor' is (I guess a function, but
> no clue about what it's signature or functionality should be) or what an
> "instance" is (but I can guess). Trouble is when you guess and things
> don't work you don't have a clue if the fault lies in your 'guess' or in
> your code. Note that it mentions 'the BROWSER variable' but does not say
Based only on my own review of the webbrowser module source, the
constructor is the '*Browser' class object [ in other words
BackgroundBrowser and not BackgroundBrowser() ], presumably it should be
a subclass of webbrowser.GenericBrowser. And instance is what you would
think, an instance of a '*Browser' class.
> what it is, what it's value should be, nor any clue about it's use.
> Probably I lack knowledge in the subject, but hey! the whole purpose of
> this module is to be able to call the browser WITHOUT any knowledge of
> the subject.
> I guess I'll have to look at the code when I have a couple of free hours.
I would highly recommend it. It's straight forward, and very readable.
A helpful tip, perhaps: one of my favorite features of ipython, allows
fairly trivial access to a module's python source (not that it's
difficult by other means, really):
In : import webbrowser
In : webbrowser??
A single question mark prints the doc string if available, where the ??
pages the source. On linux, not sure about windows, I can move around
and search for text with all the same familiar keyboard commands (less
> I'd rather go with the 'webbrowser' module than the subprocess.Popen
> option, as it 'should' abstract me from browser management. Anyway if I
I think that's the intent of the module, and a worthwhile pursuit. But,
IMHO you may find it's more work, and less flexible, than keeping track
of the browser paths yourself and calling subprocess.Popen -- on Windows
at least. YMMV. And it is certainly possible that you'll discover some
feature of the webbrowser module that I've overlooked. So, I hope you'll
report back to the list with your progress.
> can't advance through webbrowser I'll give your examples a try.
> Thanks a lot for your help.
More information about the Tutor