RussellB.
RussellB.

Reputation: 356

Open a HTML file with Python using Firefox when Firefox isn't the default browser

I've seen a few questions related to this, but I'm still having trouble.

Running the code:

>>>webbrowser.get('firefox')

errors with:

webbrowser.Error: could not locate runnable browser

To troubleshoot I ran:

>>>print(webbrowser._browser)

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

The odd thing is that I have Firefox installed, it is my default browser, and the HTML file I'm trying to opens through Python, opens with Firefox.

All would be right with the world except I need to send this program out to people who likely have IE set as their Windows default, and the HTML file has to be opened in Firefox.

Upvotes: 4

Views: 2839

Answers (3)

sangwan9
sangwan9

Reputation: 1

Add these two lines at top to register firefox

firefox_path="C:\\Program Files (x86)\\Mozilla Firefox\\firefox.exe"

this will locate your firefox executable

webbrowser.register('firefox', None,webbrowser.BackgroundBrowser(firefox_path))

Then try:

webbrowser.get('firefox') 

this worked for me in both python2 and python3

Upvotes: 0

Louis Yang
Louis Yang

Reputation: 3831

Add %s to the end of the path to open it by Firefox.

webbrowser.get('C:/Program Files (x86)/Mozilla Firefox/firefox.exe %s')

Upvotes: 1

alecxe
alecxe

Reputation: 473873

All would be right with the world except I need to send this program out to people who likely have IE set as their Windows default, and the HTML file has to be opened in Firefox.

One way to solve it is to use the selenium browser automation package. You can open local HTML files with that as well:

from selenium import webdriver

driver = webdriver.Firefox()
driver.get("file:///D:/folder/abcd.html")

Upvotes: 1

Related Questions