4

I'm trying to register the Firefox browser to run on Windows. According to the documentation for Webbrowser, "If the environment variable BROWSER exists, it is interpreted to override the platform default list of browsers, as a os.pathsep-separated list of browsers to try in order". I have the following:

import os
import webbrowser
from subprocess import call

os.environ["BROWSER"] = "C:\\FirefoxPortable\\FirefoxPortable.exe"
webbrowser.open('http://google.com')

This still opens iexplorer ( the default browser ).

Also:

>>> webbrowser._browsers
{'windows-default': [<class 'webbrowser.WindowsDefault'>, None], 'c:\\program files\\internet explorer\\iexplore.exe': [None, <webbrowser.BackgroundBrowser object at 0x04A18F90>]}
>>> webbrowser._tryorder
['windows-default', 'C:\\Program Files\\Internet Explorer\\IEXPLORE.EXE']

How Can I use Firefox here?

Source:

# OK, now that we know what the default preference orders for each
# platform are, allow user to override them with the BROWSER variable.
if "BROWSER" in os.environ:
    _userchoices = os.environ["BROWSER"].split(os.pathsep)
    _userchoices.reverse()

    # Treat choices in same way as if passed into get() but do register
    # and prepend to _tryorder
    for cmdline in _userchoices:
        if cmdline != '':
            cmd = _synthesize(cmdline, -1)
            if cmd[1] is None:
                register(cmdline, None, GenericBrowser(cmdline), -1)
    cmdline = None # to make del work if _userchoices was empty
    del cmdline
    del _userchoices

# what to do if _tryorder is now empty?
user1592380
  • 34,265
  • 92
  • 284
  • 515
  • 1
    Assuming the path given to the executable is valid, what you have should work. The source for the `webbrowser` module is included with the Python distribution and is in `C:\PythonX\Lib\webbrowser.py`, so you could make a copy of it for debugging purposes to find the problem. Look for the line `if "BROWSER" in os.environ:`. – martineau Sep 16 '14 at 19:05
  • thank you that is very useful info. looking at the source (I've included it above ) I can't see an obvious problem, although I'm suspecting it has to do with the register function . I tried to set a breakpoint within the webbrowser module code itself, but this does not cause a break. Is this possible , how do you step through more complex code with python? - Bill – user1592380 Sep 18 '14 at 15:05
  • Assuming you set the breakpoint properly, it simply sounds like the `webbrowser` module's code where it was placed was never executed -- so try to figure out way that's the case. I don't know where you put the breakpoint, but perhaps `"BROWSER"` wasn't in `os.environ`... – martineau Sep 18 '14 at 16:45
  • 1
    A different approach would be to just start Firefox yourself, rather than using the `webbrowser` module. It's easy -- there's an example of doing this with the `subprocess` module in [this answer](http://stackoverflow.com/a/25920456/355230) to the question [Opening several tabs in Firefox using Python](http://stackoverflow.com/questions/25920455/opening-several-tabs-in-firefox-using-python). – martineau Sep 18 '14 at 19:18
  • Martineau, thanks for your help. I ended up getting an answer here : http://stackoverflow.com/questions/25849813/how-to-set-the-path-to-a-browser-executable-with-python-webbrowser. Regards, - Bill – user1592380 Sep 19 '14 at 17:13
  • Bill: Good...same basic idea really, just uses `subprocess` in a different manner. Unfortunately we still don't know why setting the environment variable `"BROWSER"` wasn't work properly... – martineau Sep 19 '14 at 17:49
  • @user61629 Would be nice if you can check if reordering would work for you. – nepix32 Sep 01 '15 at 15:19

2 Answers2

5

Tried your example and got the same result: Was opening in IE, not in Firefox. Reason is, that at import time of webbrowser, the BROWSER environment variable is not yet set. By simply reordering:

import os
# put it **before** importing webbroser
os.environ["BROWSER"] = "C:\\FirefoxPortable\\FirefoxPortable.exe"
import webbrowser
# from subprocess import call

webbrowser.open('http://google.com')

it works now. I figured that by trying to set the environment variable on the command line. Note: It did not work having the path in quotes

set BROWSER=C:\FirefoxPortable\FirefoxPortable.exe

did work,

set BROWSER="C:\FirefoxPortable\FirefoxPortable.exe"

did not. Sorry for the late answer, but the diagnostics with

>>> webbrowser._browsers
>>> webbrowser._tryorder

had been very helpful, thanks.

nepix32
  • 3,012
  • 2
  • 14
  • 29
2

Try the following code:

webbrowser.register('firefox', None, webbrowser.GenericBrowser('C:\\Program Files (x86)\\Mozilla Firefox\\firefox.exe'))
a=webbrowser.get('firefox')
a.open("www.google.com")­­­­­
kenorb
  • 155,785
  • 88
  • 678
  • 743