Python :: How to open a page in Non Default Browser

I tried to create a simple script to open a locally hosted website to test css in 2 or more browsers. The default browser is IE7, and it opens the page perfectly, but when I try to open a non-default browser like Firefox or Arora, it just fails.

I use the webbrowser module and have tried this several times, as described in various sites on the Internet.

Is it possible, and if so, how?

+4
source share
3 answers

Matt is to the right, and this is 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 
+3
source

The subprocess module should provide you with what you want if you feed the subprocess to the browser path. Note that you need Python 2.4 or later to use the subprocess, but this is currently common.

Refresh - code to call the Chrome method when opening the passed 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) 
+1
source

It basically boils down to:

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

I don't know enough python to find out how the system () call is made, but it should be pretty simple.

0
source

Source: https://habr.com/ru/post/1299106/


All Articles