10

I'm trying to write a Python script to open a URL, but I keep getting errors when I try to use it:

import webbrowser

firefox = webbrowser.get('mozilla')

This is the error:

Traceback (most recent call last):
  File "C:\Users\Gelu\Documents\CSCI\Image URL Generator\src\Generator.py", line 8, in <module>
    firefox = webbrowser.get('mozilla')
  File "C:\Program Files\Python31\lib\webbrowser.py", line 53, in get
    raise Error("could not locate runnable browser")
webbrowser.Error: could not locate runnable browser

Any ideas why this isn't working?

Ryan
  • 253
  • 2
  • 4
  • 11

5 Answers5

17

if you do

import webbrowser
print webbrowser._browsers

you will get a list of the recognized browsers on your system.

Hugh Bothwell
  • 55,315
  • 8
  • 84
  • 99
8

I think you are trying to open Firefox, right?

firefox = webbrowser.get('firefox')

Works. From the docs, browser types.

user225312
  • 126,773
  • 69
  • 172
  • 181
  • Hence why I'm confused. I was reading something about how it reads the PATH file or something like that and how it isn't in it. I am not sure, I'm just generally confused why it won't work – Ryan Dec 23 '10 at 09:54
  • Ryan, I think that `mozilla` refers to SeaMonkey perhaps? I am not sure. But Firefox is `firefox` and that is why it was not working for you. It should work now. – user225312 Dec 23 '10 at 10:02
  • After some messing around with default browsers this worked, thanks! – Ryan Dec 23 '10 at 20:30
7

For me the issue was, webbrowser.py did not recognize any other browser in my windows machine. So, i had to register the browser and then launch a new tab.

import webbrowser
urL='https://www.google.com'
firefox_path="C:\\Program Files (x86)\\Mozilla Firefox\\firefox.exe"
webbrowser.register('firefox', None,webbrowser.BackgroundBrowser(firefox_path),1)
webbrowser.get('firefox').open_new_tab(urL)

Hope this helps some one.

Also some python notes for reference on what register does ,

webbrowser.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.

Yogamurthy
  • 988
  • 15
  • 22
  • This worked perfectly with google-chrome also. It did not used to need this but a windows10 update may have changed things. Printing out the list of browsers only game me "windows-default" (as per the previous answer). – Arnold Aug 31 '18 at 21:04
0

To sum up and add more tricks to the solution for future searchers of the problem:

  1. If you can't open Firefox or got an error "could not locate runnable browser" (in webbrowser.py), first of all please check whether Python sees any browsers (you should get a list of browsers, as mentioned by @Hugh Bothwell above, but for me it worked only with parentheses for print

    import webbrowser print (webbrowser._browsers)

  2. If there is no Firefox or you got an empty list, you should add a folder of a browser to System Path (in this example for Firefox) (this solution was given by @ntk4 here)

Windows7 -> Start -> Control Panel -> System -> Advanced System Settings (on the left) -> pop-up window "System Properties" appears -> Advanced -> click on "Environment Variables" in the bottom right corner -> in the pop-up appeared in "System variables" field find "Path" and click on "Edit" button under the field -> in the end of the "Variable value" field add

;C:\Program Files\Mozilla Firefox\firefox.exe

->click OK and apply in the next window (It may be different on your machine or OS) -> Restart your laptop/PC

  1. Register your browser in Python (as answered by @Ali Moshiri here)

    import webbrowser urL='https://www.python.org' mozilla_path="C:\Program Files\Mozilla Firefox\firefox.exe" webbrowser.register('firefox', None,webbrowser.BackgroundBrowser(mozilla_path)) webbrowser.get('firefox').open_new_tab(urL)

This magic worked for me and finally I can use a browser I want and not the default one :)

AlexStox
  • 61
  • 9
0

I could not get webbrowser to locate my default browser and this is how i fixed it

import webbrowser
    url = input("Enter Website Url: ")
    firefox_path = "C:\\Program Files\\Mozilla Firefox\\firefox.exe" #define the Path to firefox
    webbrowser.register('firefox', None,webbrowser.BackgroundBrowser(firefox_path))
    webbrowser.get('firefox').open_new_tab(url)

basically an updated version of a great answer already given on this post this can also be used for "google-chrome" refer too https://docs.python.org/3/library/webbrowser.html?highlight=webbrowser#module-webbrowser under webbrowser.register for more info

Tyler R
  • 1
  • 1