开发者

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

开发者 https://www.devze.com 2022-12-17 15:05 出处:网络
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 def开发者_JAVA百科ault browser is IE7 and it opens the page fine but when I try t

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 def开发者_JAVA百科ault 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.

0

精彩评论

暂无评论...
验证码 换一张
取 消