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

Question:

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?

Asked By: Andy Crouch

||

Answers:

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.

Answered By: Morfildur

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)
Answered By: GreenMatt

Matt’s right and it’s a pretty useful module to know…

18.1. subprocess

IDLE 2.6.2      
>>> import subprocess
>>> chrome = 'C:UsersTedAppDataLocalGoogleChromeApplicationchrome.exe'
>>> chrome_args = 'www.rit.edu'
>>> spChrome = subprocess.Popen(chrome+' '+chrome_args)
>>> print spChrome.pid
2124
Answered By: tethys

Sometimes, get() function of webbrowser doesn’t work properly. In that case, you can use register() to define your own browser.

import webbrowser
browser_path="<path_to_your_browser_executable>"
webbrowser.register('my_browser', None, webbrowser.BackgroundBrowser(browser_path))
webbrowser.get('my_browser').open("stackoverflow.com")
Answered By: FanaticExplorer
Categories: questions Tags: ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.