0

I am currently looking for a solution to click on Sava button in chrome print preview window with selenium Java.

Is there any way we can handel chrome print preview page? I have tried with the Robot class, but it seems not reliable/stable for my application.

Could you please someone help me to achieve this with selenium Java.

Anil Kumar P
  • 53
  • 1
  • 9

2 Answers2

0

I fail to see any "Save" button on print preview page for Chrome browser

enter image description here

Here is how you can click "Cancel" button, I believe you will be able to amend the code to match your requirements:

  1. First of all make sure to wait for the preview page to be available using Explicit Wait
  2. Change the context to the print preview window via WebDriver.switchTo() function
  3. All the elements at the print preview page are hidden in ShadowDom therefore you will need to:

    • Locate the element which is the first parent of the element you're looking for
    • Get its ShadowRoot property and cast the result to a WebElement
    • Use the WebElement.findElement() function to locate the next parent
    • repeat above steps until you reach the desired button
  4. Example code just in case:

    new WebDriverWait(driver, 10).until(ExpectedConditions.numberOfWindowsToBe(2));
    driver.switchTo().window(driver.getWindowHandles().stream().skip(1).findFirst().get());
    WebElement printPreviewApp = driver.findElement(By.tagName("print-preview-app"));
    WebElement printPreviewAppConten = expandShadowRoot(printPreviewApp, driver);
    WebElement printPreviewSidebar = printPreviewAppConten.findElement(By.tagName("print-preview-sidebar"));
    WebElement printPreviewSidebarContent = expandShadowRoot(printPreviewSidebar, driver);
    WebElement printPreviewHeader = printPreviewSidebarContent.findElement(By.tagName("print-preview-header"));
    WebElement printPreviewHeaderContent = expandShadowRoot(printPreviewHeader, driver);
    printPreviewHeaderContent.findElements(By.tagName("paper-button")).get(1).click();
    

    where expandShadowRoot function looks like:

    private WebElement expandShadowRoot(WebElement parent, WebDriver driver) {
        return (WebElement) ((JavascriptExecutor) driver).executeScript("return arguments[0].shadowRoot", parent);
    }
    
Dmitri T
  • 159,985
  • 5
  • 83
  • 133
0

Save button will not work because page is not part of webpage. selenium only support web based application.

But you can use SIKULI to handle above scenario.

MadProgrammer
  • 513
  • 5
  • 18