How can I tell Selenium to press cancel on a print popup in Chrome 75?
Asked Answered
U

4

1

I have been using the the suggested solution from : https://mcmap.net/q/1586369/-how-can-i-tell-selenium-to-press-cancel-on-a-print-popup

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
Uncircumcision answered 24/7, 2019 at 18:36 Comment(3)
Have you updated chromedriver to match chrome v75?Dissertation
I believe you can just use history.back() for this.Selfwill
@Dissertation Yes, using correct Chromedriver. The brower version and driver version match.Uncircumcision
S
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()
Sherronsherry answered 26/7, 2019 at 10:44 Comment(2)
How did you figure out there was an additional element?Uncircumcision
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 .Sherronsherry
P
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();");
Prosper answered 24/10, 2019 at 15:28 Comment(0)
F
0

Running this code before print popup :

executor.executeScript("window.print = function(){ return false;};");
Fritzsche answered 25/7, 2019 at 15:9 Comment(1)
Nope. Just tried that. The print preview screen still comes up.Uncircumcision
S
0

I found this solution works for chrome version 120:

After the print popup your WebdDriver doesn't know which window the operating system consider active, so you have to switch your WebdDriver to the new window:

Set<String>windows = driver.getWindowHandles();
System.out.println(windows);
Iterator<String>iterator = windows.iterator();
String parentWindow = iterator.next();
String childWindow = iterator.next();
driver.switchTo().window(child);

After that, since the print window is a Shadow DOM, you have to use JavaScript Excuter to enter the inner DOM to locate the cancel button:

js.executeScript( "document.querySelector('print-preview-app').shadowRoot.querySelector('#sidebar').shadowRoot.querySelector('print-preview-button-strip').shadowRoot.querySelector('.cancel-button').click();");

Hope I helped.

Specter answered 12/2 at 13:58 Comment(1)
Your answer could be improved with additional supporting information. Please edit to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers in the help center.Lamellibranch

© 2022 - 2024 — McMap. All rights reserved.