How to set the path to a browser executable with python webbrowser

Question:

I am trying to build a utility function to output beautiful soup code to a browser I have the following code:

def bs4_to_browser(bs4Tag):

    import os
    import webbrowser

    html= str(bs4Tag)

    # html = '<html> ...  generated html string ...</html>'
    path = os.path.abspath('temp.html')
    url = 'file://' + path

    with open(path, 'w') as f:
        f.write(html)
    webbrowser.open(url)
    return

This works great and opens up the HTML in the default browser. However I would like to set the path to a portable firefox executable which is at:

F:FirefoxPortablefirefox.exe

I am using win7. How to I set the path to the portable firefox executable?

Asked By: user1592380

||

Answers:

You could start your portable Firefox directly with the url as an argument instead.

from subprocess import call
call(["F:\FirefoxPortable\firefox.exe", "-new-tab", url])
Answered By: Lallen

I know the question is old but here a code working with webbrowser and Python 3.11

myfirefox = webbrowser.Mozilla("F:\FirefoxPortableESR\FirefoxPortable.exe")
myfirefox.open(url)

As you will see, it works even if the .exe is not the "real" firefox.

Answered By: Fanch