How to set the path to a browser executable with python webbrowser
Asked Answered
C

2

0

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:\FirefoxPortable\firefox.exe

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

Cedilla answered 15/9, 2014 at 13:58 Comment(2)
I remember you've used selenium for this. Why are you switching to webbrowser?Communitarian
regarding #25736991 , thanks for your help on that Alex. I was not able to get it working using selenium perhaps related to firefox browser/webdriver incompatabilities. This seems to be working immediatelyCedilla
R
1

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])
Rustproof answered 15/9, 2014 at 14:5 Comment(2)
My only question on this is I would like to open a new tab each time rather than an entire new browser. the webrowser module does appear to allow control of tabsCedilla
List of available Firefox command line arguments, should work with -new-tabRustproof
C
0

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.

Clarkclarke answered 19/2, 2023 at 17:20 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.