5

I'm trying to write a script that will launch firefox for me, open up google in a new tab, and be able to do a search (for instance, www.espn.com). I'm currently trying to achieve this by using the webbrowser module, however I run into an error each time that I try launching Firefox from the script. Also, firefox is not my default browser.

import webbrowser
webbrowser.get('firefox').open_new_tab('http://www.google.com')

Whenever I run this I get the following error:

Traceback (most recent call last):
  File "C:/Python33/test Bing.py", line 6, in <module>
    webbrowser.get('firefox').open_new_tab('http://www.google.com')
  File "C:\Python33\lib\webbrowser.py", line 53, in get
    raise Error("could not locate runnable browser")
webbrowser.Error: could not locate runnable browser

I'm unsure why the script is struggling to locate the firefox.exe I have also tried specifying in 'firefox' the actual location of firefox.exe in c: however I still get the same error.

I'm sure that there's a small error in my code that I currently can't see, if someone could help point out what I'm doing wrong I'd greatly appreciate it!

Valrok
  • 1,514
  • 7
  • 30
  • 49

2 Answers2

7

I have Firefox installed on my Windows machine as well, and have the same error.

If you run the following two lines in IDLE:

import webbrowser
print webbrowser._browsers # or print(webbrowser._browsers) for Python 3.x

Then you'll get a dict of available browser controllers, as said in the source code. On my system it prints:

{'windows-default': [<class 'webbrowser.WindowsDefault'>, None], 
    'c:\\program files\\internet explorer\\iexplore.exe': [None, <webbrowser.BackgroundBrowser object at 0x01BAF6B0>]
}

I have IE, Chrome and Firefox, but only "Default" and "Internet Explorer" appear here. According to the documentation, the keys 'firefox' and 'mozilla' should work, but they are absent here.

From lines 539-563 in the source code, Python will only register a browser if its corresponding key (e.g. 'firefox' or 'chrome') is a valid command using _iscommand(cmd).

I added the Firefox path to %path% and restarted IDLE. _iscommand('firefox') returns True and webbrowser.get('firefox) returns a <webbrowser.BackgroundBrowser object at 0x01BDF7F0>. However, webbrowser._iscommand("chrome") still returns False and webbrowser.get("chrome") still throws the exception.

Conclusion

You'll have to add the Firefox path to the %path% variable first, or make the assumption that Firefox is the default browser.

SimonT
  • 2,219
  • 1
  • 18
  • 32
  • I actually just ran the same thing you did to get a list of available browsers and also only got for IE. I haven't coded in python in several years, so I'm a little unsure of where I can add the firefox path. – Valrok Aug 26 '13 at 14:54
  • 1
    The `%path%` variable is for the whole System, not just for Python. You can find a guide at http://www.computerhope.com/issues/ch000549.htm. – SimonT Aug 26 '13 at 15:02
2

Make sure the Firefox executable is on the path (%PATH% on Windows, $PATH on Linux).

Aaron Digulla
  • 321,842
  • 108
  • 597
  • 820