简体   繁体   中英

Python :: How to open a page in the Non Default browser

I was trying to create a simple script to open a locally hosted web site for testing the css in 2 or more browsers. The default browser is IE7 and it opens the page fine but when I try to open a non default browser such as Firefox or Arora it just fails.

I am using the webbrowser module and have tried this several way as detailed in various sites across the web.

Is it possible and if so how?

Matt's right and it's a pretty useful module to know...

18.1. subprocess

IDLE 2.6.2      
>>> import subprocess
>>> chrome = 'C:\Users\Ted\AppData\Local\Google\Chrome\Application\chrome.exe'
>>> chrome_args = 'www.rit.edu'
>>> spChrome = subprocess.Popen(chrome+' '+chrome_args)
>>> print spChrome.pid
2124

The subprocess module should provide what you want if you feed subprocess the path to the browser. Note that you need Python 2.4 or later to use subprocess, but that's common nowadays.

Update - code for a method to call Chrome, while opening a passed in URL:

def startChrome(url):
    """ Calls Chrome, opening the URL contained in the url parameter. """
    executable = 'path-to-chrome'    # Change to fit your system
    cmd = ' '.join([executable, url])
    browswer_proc = subprocess.Popen(cmd, shell=True)

This basically boils down to:

- run 'firefox "url"'
- run 'iexplore "url"'
- run 'other_browser "url"'

I don't know enough python to know how the system() call is implemented there but it should be quite simple.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM