That is beyond what Selenium can do at least now.
You have to use another way instead.
Here is a simplified python implementation invoking AutoHotkey.
You need to have AutoHotkey installed first.
Have the following 2 files in one folder and run Chrome_Print.py, then Selenium will open StackOverflow, wait 500ms and open the Chrome Print window, then wait 2 seconds and exit it.
You can create a Java implementation which is similar.
As you can see, this approach can be used in an automated manner, but the sleep time is hard-coded and is dependent on specific machine. Also it requires always having window focused when it is processing.
Chrome_Print.py
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.chrome.options import Options
import sys, os, psutil, win32process, win32gui, win32api, time
def get_hwnds_from_pid(pid):
def EnumWindowsProc(hwnd, hwnds):
if win32gui.IsWindowVisible(hwnd) and win32gui.IsWindowEnabled(hwnd):
returned_tid, returned_pid = win32process.GetWindowThreadProcessId(hwnd)
if returned_pid == pid:
hwnds.append(hwnd)
return True
hwnds = []
win32gui.EnumWindows(EnumWindowsProc, hwnds)
return hwnds
driveroptions = Options()
driveroptions.add_argument('--incognito')
driveroptions.add_argument('--disable-infobars')
driveroptions.add_argument('--start-maximized')
driver = webdriver.Chrome(options=driveroptions)
driver.set_page_load_timeout(40)
wait = WebDriverWait(driver, 40)
driver.get("https://www.stackoverflow.com")
wait.until(EC.presence_of_element_located((By.XPATH, '/html/body/div[@class="container"]/div[@id="content"]')), "Waiting For Content Timeout")
current_python_pid = os.getpid()
current_python_process = psutil.Process(current_python_pid)
chromedriver_process_list = current_python_process.children(recursive=False)
chromedriver_process = chromedriver_process_list[0]
chrome_process_list = chromedriver_process.children(recursive=False)
chrome_process = chrome_process_list[0]
chrome_hwnd_list = get_hwnds_from_pid(chrome_process.pid)
win32api.ShellExecute(chrome_hwnd_list[0], 'open', "ChromePage_Print.ahk", str(chrome_hwnd_list[0]), os.getcwd(), 1)
time.sleep(4)
driver.quit()
ChromePage_Print.ahk
#NoEnv ; Recommended for performance and compatibility with future AutoHotkey releases.
; #Warn ; Enable warnings to assist with detecting common errors.
SendMode Input ; Recommended for new scripts due to its superior speed and reliability.
SetWorkingDir %A_ScriptDir% ; Ensures a consistent starting directory.
Chrome_HWND = %1%
WinActivate, ahk_id %Chrome_HWND%
Sleep, 500
SendInput ^p
Sleep, 2000
SendInput {Esc}
Sleep, 800