Problems Opening Firefox

I am 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 mistake:

 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 is not working?

+8
python browser firefox
source share
3 answers

I think you are trying to open Firefox, right?

 firefox = webbrowser.get('firefox') 

Working. From docs browser types.

+8
source share

if you do

 import webbrowser print webbrowser._browsers 

You will get a list of recognized browsers on your system.

+13
source share

For me, the problem was that webbrowser.py did not recognize any other browser on my Windows machine. So, I had to register a browser and 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 someone.

Also some python notes for help on what the register does,

webbrowser.register (name, constructor [, instance]) ΒΆ

Register your browser type name. Once a browser type is registered, the get () function can return a controller for that browser type. If an instance is not specified or is None, the constructor will be called without parameters to create an instance if necessary. If an instance is provided, the 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 non-empty argument corresponding to the name of the handler you are declaring.

+2
source share

Source: https://habr.com/ru/post/650405/


All Articles