1

I need to click on the cancel button after opening the print window in chrome.

I tried below code but it is not working.

driver.switchTo().window(driver.getWindowHandles().toArray()[1].toString());
JavascriptExecutor executor = (JavascriptExecutor) driver.getWebDriver();
executor.executeScript("document.querySelector(\"print-preview-app\").shadowRoot.querySelector(\"print-preview-header\").shadowRoot.querySelector(\"paper-button.cancel-button\").click();");
SiKing
  • 10,003
  • 10
  • 39
  • 90

3 Answers3

0

Interacting with the print window, is, I believe, not currently possible with selenium. See this question for a detailed description and a possible workaround with another testing tool, Robot.

C. Peck
  • 3,641
  • 3
  • 19
  • 36
0

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

Steven Liang
  • 355
  • 3
  • 10
0

We're doing this right now in our tests, and the only big warning here is that the HTML for this panel seems to change pretty frequently, so be aware of that. The code that you need for Version 75.0.3770.100 (Official Build) (64-bit) is here:

executor.executeScript(
  "document" 
  + ".querySelector('print-preview-app')
  + ".shadowRoot"
  + ".querySelector('print-preview-sidebar')
  + ".shadowRoot"
  + ".querySelector('print-preview-header')
  + ".shadowRoot"
  + ".querySelector('.cancel-button')
  + ".click();"
);
Mike Burton
  • 3,010
  • 24
  • 33