1

I have been using the the suggested solution from : https://stackoverflow.com/a/41670021/4633408

And it worked perfectly since Chrome 71.

executor.executeScript("document.querySelector(\"print-preview-app\").shadowRoot.querySelector(\"print-preview-header\").shadowRoot.querySelector(\"paper-button.cancel-button\").click();");

But it no longer works.

Has anyone figured out a way to click, "Cancel" in the print preview for Chrome 75?

I caught the exception and it simply says:

 e:org.openqa.selenium.WebDriverException: unknown error: Cannot read property 'shadowRoot' of null
Anton
  • 761
  • 17
  • 40

3 Answers3

7

Tested this on Version 75.0.3770.142 (Official Build) (64-bit) .They have additional element now

<print-preview-sidebar id="sidebar"></print-preview-sidebar>

For testing in console

document.querySelector("print-preview-app").shadowRoot.querySelector("print-preview-sidebar").shadowRoot.querySelector("print-preview-header").shadowRoot.querySelector("paper-button.cancel-button").click()

with executor.executeScript

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

EDIT :: For Version 79.0.3945.88 (Official Build) (64-bit)

document.querySelector("print-preview-app").shadowRoot.querySelector("print-preview-sidebar").shadowRoot.querySelector("print-preview-button-strip").shadowRoot.querySelector("cr-button.cancel-button").click()
Rahul L
  • 4,249
  • 15
  • 18
  • How did you figure out there was an additional element? – Anton Jul 26 '19 at 13:24
  • 1
    Right click on the 'cancel' and click on inspect .it will open chrome Dev tool then you can check print preview apps DOM. The way we normally do it for html . – Rahul L Jul 26 '19 at 14:09
1

In Chrome 77 the html elements of print dialog have changed again. And changed again in Chrome 78. Very annoying when on our jenkins the version is a few numbers behind the latest on my laptop. (I hope they soon enable that jenkins for docker containers)

For Chrome 78.0.3904.70:

testing in console:

document.querySelector("print-preview-app").shadowRoot.querySelector("print-preview-sidebar").shadowRoot.querySelector("print-preview-button-strip").shadowRoot.querySelector("cr-button.cancel-button").click()

executor.executeScript:

executor.executeScript("document.querySelector(\"print-preview-app\").shadowRoot.querySelector(\"print-preview-sidebar\").shadowRoot.querySelector(\"print-preview-button-strip\").shadowRoot.querySelector(\"cr-button.cancel-button\").click();");
0

Running this code before print popup :

executor.executeScript("window.print = function(){ return false;};");
Isa Ataseven
  • 166
  • 1
  • 10