Python subprocess launch new chrome tab

115 views Asked by At

I have a little pythons script that launches all tabs on startup that I need, in the end there should be a new tab page:

import requests
import subprocess
import time

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

if is_this:
    subprocess.Popen([chrome_path, 'example.com'])
    time.sleep(5)
    subprocess.Popen([chrome_path, 'https://exmaple.com'])
    time.sleep(0.2)
    subprocess.Popen([chrome_path, 'http://guthib.com'])
    time.sleep(0.2)
    subprocess.Popen([chrome_path, 'chrome://newtab'])
else:
    subprocess.Popen([chrome_path, 'https://github.com'])
    time.sleep(0.2)
    subprocess.Popen([chrome_path, 'http://testurl.com'])
    time.sleep(0.2)
    subprocess.Popen([chrome_path, 'chrome://newtab'])

That all works fine, except the chrome://newtab opens a new chrome window instead of a new-tab-page in the same window.

I literally searched all over the web and could not find a solution, except that using selenium with webdriver, however with them I get soo many problems:

It's not launching with my chrome profile It shouws a banner that this browser is running aautomated Windows Defender flaggs it. newtab als does not work there.

Thanks for any help.

2

There are 2 answers

3
Jakob Bagterp On

Would you consider using the Browserist extension to Selenium?

In full disclosure, I'm the author of this package. Browserist is lightweight, less verbose extension of the Selenium web driver that makes browser automation even easier. Simply install the package with pip install browserist and you're ready to go.

Maybe something like this could solve your problem?

from browserist import Browser

with Browser() as browser:
    browser.open.url("https://example.com")
    browser.window.open.new_tab("https://google.com", "tab_2")
    browser.window.open.new_tab("https://github.com", "tab_3")
    browser.window.open.new_tab("http://testurl.com", "tab_4")
    browser.window.open.new_tab("chrome://newtab", "tab_5")

    print("About to quit the browser. Press Y to continue.")
    while browser.prompt.proceed_yes_or_no() is False:
        print("About to quit the browser. Press Y to continue.")
        pass

How it looks like:

enter image description here

Notes:

  • When running from a terminal, the browser will be kept open for you to interact and check each tab until you quit by pressing Y.
  • Small detail: It seems like you're running Chrome on a Mac. Chrome is default for Browserist on macOS. If you run Windows, Edge will be default, and so you may need to adjust the settings a little if Chrome is preferred.
1
Jakob Bagterp On

How about opening multiple tabs from a folder of links? It's probably easier to maintain than in code while you retain your Chrome profile and options standard options.

After you've created a bookmark folder, right click on the folder and then select “Open all”.

Right click on shortcuts folder in Chrome

There a guide here: https://medium.com/@partizion/how-to-open-multiple-tabs-in-chrome-eeaca29563d