如何在Chrome的打印预览页面上单击取消按钮?

时间:2019-03-05 00:07:54

标签: java selenium google-chrome

打开镀铬的打印窗口后,我需要单击“取消”按钮。

我尝试了下面的代码,但是没有用。

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();");

3 个答案:

答案 0 :(得分:0)

我相信,硒目前无法与打印窗口进行交互。有关详细说明以及使用其他测试工具Robot的可能解决方法,请参见this问题。

答案 1 :(得分:0)

Selenium至少现在无法做到。 您必须改用另一种方式。

这是调用AutoHotkey的简化的python实现。 您需要先安装AutoHotkey

将以下2个文件放在一个文件夹中并运行Chrome_Print.py,然后Selenium将打开StackOverflow,等待500毫秒并打开Chrome打印窗口,然后等待2秒钟并退出。

您可以创建类似的Java实现。

如您所见,这种方法可以自动使用,但是睡眠时间是硬编码的,并且取决于特定的机器。此外,它还要求在处理时始终使窗口聚焦。

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

答案 2 :(得分:0)

我们现在在测试中正在执行此操作,这里唯一的大警告是此面板的HTML似乎经常更改,因此请注意这一点。 Version 75.0.3770.100 (Official Build) (64-bit)所需的代码在这里:

executor.executeScript(
  "document" 
  + ".querySelector('print-preview-app')
  + ".shadowRoot"
  + ".querySelector('print-preview-sidebar')
  + ".shadowRoot"
  + ".querySelector('print-preview-header')
  + ".shadowRoot"
  + ".querySelector('.cancel-button')
  + ".click();"
);