Ryan
Ryan

Reputation: 253

Problems Opening Firefox

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?

Upvotes: 10

Views: 13670

Answers (5)

Tyler R
Tyler R

Reputation: 1

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

Upvotes: 0

AlexStox
AlexStox

Reputation: 71

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 :)

Upvotes: 0

Yogamurthy
Yogamurthy

Reputation: 998

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.

Upvotes: 7

Hugh Bothwell
Hugh Bothwell

Reputation: 56694

if you do

import webbrowser
print webbrowser._browsers

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

Upvotes: 17

user225312
user225312

Reputation: 131807

I think you are trying to open Firefox, right?

firefox = webbrowser.get('firefox')

Works. From the docs, browser types.

Upvotes: 8

Related Questions