Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

python

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?

like image 846
Andy Crouch Avatar asked Oct 15 '22 10:10

Andy Crouch


2 Answers

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
like image 156
tethys Avatar answered Oct 18 '22 14:10

tethys


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)
like image 21
GreenMatt Avatar answered Oct 18 '22 13:10

GreenMatt