What is the proper way I can invoke firefox from a python3 program?
Question: I'm trying to start firefox with an internet page by calling it in python 3 as
an argument to os.system or os.startfile.

The internet page I want to start is <https://schwab.com>

I can't bring it up at the command line with

C:\Python34\hsf\WSC>C:\Program Files(x86)\Mozilla Firefox\firefox.exe
<https://schwab.com>

It chokes on the spaces.

But I can by using

C:\Progra~2\Mozill~1\firefox.exe <https://schwab.com>

That works fine at the command line

So I put that address as the argument to os.system in my python program, and
got the error:

'C:\Progra~2\Mozill~1' is not recognized as an internal or external command,
operable program or batch file.

I tried it in os.startfile and got the error message:

Exception in Tkinter callback Traceback (most recent call last): File
"C:\Python34\lib\tkinter__init__.py", line 1482, in **call** return
self.func(*args)

File "C:\Python34\hsf\WSC\fm.py", line 59, in Schwab

    
    
    res=os.startfile('C:\Progra~2\Mozill~1\firefox.exe https://schwab.com')
    

FileNotFoundError: [WinError 2] The system cannot find the file specified:

'C:\Progra~2\Mozill~1\x0cirefox.exe <https://schwab.com>'

Note that it echoed my argument correctly, but the FileNotFoundError has
inserted the string x0cire between '\' and 'firefox'

I deleted and retyped the '\f', and got the same erroroneous string inserted.

To avoid the path, I copied firefox.exe into my folder, but it won't run
outside its native milieu.

What is the proper way I can invoke firefox from a python3 program?


Answer: It depends what you want to do with this site. If all you want to do is open
the page use the [webbrowser
module](https://docs.python.org/3/library/webbrowser.html#webbrowser.open) to
open the url.

    
    
    import webbrowser
    
    webbrowser.open('https://www.schwab.com/')
    

If you need to something more complicated, you can use the
[Selenium](http://selenium-python.readthedocs.org/) module to interact with
the page in pretty much anyway you need.

    
    
    from selenium import webdriver
    from selenium.webdriver.common.keys import Keys
    
    driver = webdriver.Firefox()
    driver.get("http://www.python.org")
    assert "Python" in driver.title
    elem = driver.find_element_by_name("q")
    elem.send_keys("selenium")
    elem.send_keys(Keys.RETURN)
    driver.close()
    

