3

I'm opening a new IE window with this:

subprocess.Popen(r'"' + os.environ["PROGRAMFILES"] + 
'\Internet Explorer\IEXPLORE.EXE" ' + Call_URL)

This is fine when IE is closed, but even when it's open this spawns a new window. How can I open just a new tab? If possible I'd like to use the standard browser - however I couldn't figure out how to do that either.

Note: I can't use webbrowser and os has no .startfile. I had no luck with os.popen either (using Jython 2.5.3b1).

Lightness Races in Orbit
  • 378,754
  • 76
  • 643
  • 1,055
MapEngine
  • 553
  • 1
  • 9
  • 21

2 Answers2

4

Since you also wanted a standard browser am giving an example to open a new tab with chrome. If chrome is not open already it will open and then navigate to the URL.

import subprocess
subprocess.Popen("start chrome /new-tab www.google.com",shell = True)

This works. Please try and let me know if this is what you wanted.

Another one without hardcoding the Call_URL

import subprocess
Call_URL = "www.google.com"
mycmd = r'start chrome /new-tab {}'.format(Call_URL)
subprocess.Popen(mycmd,shell = True) 

Are you expecting something like this?

DineshKumar
  • 1,599
  • 2
  • 16
  • 30
  • Thanks. I'm getting some errors, though. On Shell = True: `TypeError: unsupported operand type(s) for +: 'NoneType' and 'list'` On Shell = False or Shell parameter removed: `OSError: Cannot run program "start" (in directory "C:\Users\Admin\Desktop"): CreateProcess error=2, The system cannot find the file specified` This works: `subprocess.Popen(r'"C:\Users\Admin\AppData\Local\Google\Chrome\Application\chrome.exe" /new-tab ' + Call_URL)`.. which is a little improvement I guess but doesn't call the user's standard browser. (Christ, does the text editor on stack **suck**) – MapEngine Mar 14 '16 at 13:33
  • @MapEngine Call_URL should be mentioned as I have mentioned www.google.com. It cannot be passed after the command. – DineshKumar Mar 14 '16 at 13:51
  • Doesn't work in Jython (no .format). Also doesn't launch the standard browser. I basically want to do "go to google.com using whatever browser you have". Since the above doesn't work in my case, I rewrote it to at least be user-independent: `subprocess.Popen(r'"'+ 'C:\\Users\\' + os.environ["USERNAME"] + '\AppData\Local\Google\Chrome\Application\chrome.exe" /new-tab ' + Call_URL)` – MapEngine Mar 14 '16 at 20:29
0

Keep It Simple & Smart (updated)... programmatic and use a terminator()!

Here is simple answer to launch, track, and terminate a new Chrome browser instance. It launches a new process for a Chrome instance, launches additional tabs into that new Chrome webbrowser instance, and finally using "terminate()" when finished to close the original browser launched by the subprocess() and its webbrowser child tabs. This works even when there is an existing Chrome browser process running.

The standard path (user below) for Chrome.exe on Windows 10 is (usually): "C:\Program Files\Google\Chrome\Application\chrome.exe"

The code should always open a new Chrome window, even if Chrome is already running. The package "subprocess" is mandatory instead of os.system, or else it will not launch a new chrome window.

Advantages of this programmatic approach:

(1) subprocess() has a process ID, useful to track and close the browser started in the subprocess.

(2) All child tabs started within the subprocess.Popen() will be closed when the parent subprocess is terminated.

N.B. If there is an pre-existing browser instance running, my_chrome_process.terminate() will NOT terminate it; it will terminate only the instance started by the subprocess.Popen() code below. This is the expected behavior.

import subprocess
url1 = r'https://www.python.org'
url2 = r'https://github.com/'
url3 = r'https://stackoverflow.com/questions/22445217/python-webbrowser-open-to-open-chrome-browser'
url4 = r'https://docs.python.org/3.3/library/webbrowser.html'

chrome_path = r'C:\Program Files\Google\Chrome\Application\chrome.exe'

my_chrome_process   = subprocess.Popen(chrome_path, shell=False)
print(f'Process ID: {my_chrome_process.pid}')   # Uncomment this line if you want to see PID in Console.

import webbrowser
webbrowser.register('chrome', None, webbrowser.BackgroundBrowser(chrome_path))
webbrowser.get('chrome').open_new_tab(url1)
webbrowser.get('chrome').open_new_tab(url2)
webbrowser.get('chrome').open_new_tab(url3)
webbrowser.get('chrome').open_new_tab(url4)

my_chrome_process.terminate()

# If for any reason, my_chrome_process.terminate() does not work, then use the following os.system() code to kill the browser started using subprocess().   
# See https://stackoverflow.com/questions/68540790/popen-kill-not-closing-browser-window for more information. 

import os
os.system("Taskkill /PID %d /F" % my_chrome_process.pid)
Rich Lysakowski PhD
  • 2,702
  • 31
  • 44