As explained in Bakuriu's answer, --new-window
creates a new window, but under an existing instance of firefox, if there is one. If there is no existing instance, a new one is created.
It is possible to use -new-instance
to tell Firefox to start a new instance for a different firefox user profile. The profile must already exist and this is limited to one instance per profile. A new profile can be created interactively with firefox -P -new-instance
, and a new instance can then be started with firefox -P <profile_name> -new-instance
. Mozilla's profile documentation is here.
It should be possible to programmatically create the profile - after all it is just a directory and an entry in the ~/.mozilla/profiles.ini
file. Of course, this is for Firefox only, chrome might be completely different (or impossible?). An example:
import tempfile
import subprocess
import shutil
import time
import ConfigParser
MOZILLA_PROFILES_INI = '.mozilla/firefox/profiles.ini'
PROFILE_SECTION = 'Profile1'
URL = 'http://www.hckrnews.com'
profile_dir = tempfile.mkdtemp()
# quick and dirty add new profile to profiles.ini, or update if already present.
config = ConfigParser.SafeConfigParser()
config.optionxform = str # preserve case of option names
config.read(MOZILLA_PROFILES_INI)
try:
config.add_section(PROFILE_SECTION)
except ConfigParser.DuplicateSectionError, exc:
pass
config.set(PROFILE_SECTION, 'Name', 'temp_profile')
config.set(PROFILE_SECTION, 'IsRelative', '0')
config.set(PROFILE_SECTION, 'Path', profile_dir)
# write out config with hack to remove added spaces around '=' - more care needed here as it just overwrites the global mozilla config!
class NoSpaceWriter(file):
def write(self, s):
super(NoSpaceWriter, self).write(s.replace(' = ', '='))
with NoSpaceWriter(MOZILLA_PROFILES_INI, 'w') as profiles_ini:
config.write(profiles_ini)
p = subprocess.Popen(['firefox', '-P', 'temp_profile', '-new-instance', URL])
time.sleep(10)
p.terminate()
shutil.rmtree(profile_dir, True)