使用硒自动执行“打印”功能

时间:2019-05-26 07:52:43

标签: java selenium webdriver

我必须使用“ Java”和“ Selenium”自动化“打印”屏幕。但是我无法单击“取消”或“打印”按钮。

void print() {

          driver.switchTo().window(driver.getWindowHandles().stream().
          filter(handle 
          -> !handle.equals(driver.getWindowHandle())).findAny().get());

           WebElement printPreviewApp = 
           driver.findElement(By.xpath("//print- 
           preview-app"));

           WebElement printPreviewAppContent = (WebElement) 
           ((JavascriptExecutor) driver).executeScript("return 
           arguments[0].shadowRoot", printPreviewApp);

           WebElement printPreviewHeader = 
           printPreviewAppContent.findElement(By.cssSelector("print- 
           preview-header"));

           WebElement printPreviewHeaderContent = (WebElement) 
           ((JavascriptExecutor) driver).executeScript("return 
           arguments[0].shadowRoot", printPreviewHeader);

           WebElement cancelButton = 
           printPreviewHeaderContent.findElement(By.cssSelector("paper- 
           button[class*=cancel]"));
           cancelButton.click();
}

我也尝试了以下代码,

void print() {

      WebElement print_button = driver.findElement(By.cssSelector("..."));

     // click on the print button and wait for print to be called
     driver.manage().timeouts().setScriptTimeout(10, TimeUnit.SECONDS);
     ((JavascriptExecutor)driver).executeAsyncScript(
         "var callback = arguments[1];" +
         "window.print = function(){callback();};" +
         "arguments[0].click();"
         , print_button);
}

我希望单击“打印”和“取消”按钮。但是,它不是单击按钮Sample print screen

0 个答案:

没有答案