How can I open a new browser tab with subprocess?

2019-09-05 13:45发布

问题:

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).

回答1:

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?