0

I want to disable or cancel print window which automatically pops up when the page loads using Selenium.

The best I've reached is to press escape key using Robot class. However that also doesn't close the popup all the time. It works 80% of the times. Currently I'm pressing three times escape key after waiting for 1 second after page load.

I've found that following two preferences we can add (firefox):

options.addPreference("print.always_print_silent", true)
options.addPreference("print.show_print_progress", false)

This doesn't allow the print dialog to show but the print is queued as it works silently.

I've also tried the following code which overrides window.print function.

js.executeScript("window.print=function(){};")

This works for the pages where we can disable the printing before clicking on an element which will print something. In my case there is no such button as the print window automatically triggers on page load.

Need some guidance as to how I should disable print window. I need help with Firefox and Selenium. (Google Chrome is optional)

Also, I don't know the JS code of the target website and don't know which JS is triggering the on page load event. I tried to identify but wasn't able to get it. The JS code of website is obfuscated as well.


I have found the reason why print windows pops up on page load. It is because of the following JS codebase:

            n.iframeNode.on('load', function () {
                n.iframeNode[0].elem.contentWindow.print()
            }),

The following options are also what I've tried:

1) Disabling the JS itself using javascript.enabled flag. The site doesn't even load when JS is disabled.

2) Tried to disable the JS events so that the print window event doesn't trigger using dom.window.event.enabled. That is also not working (don't know if this is the right property).

3) Looked at the proxy concept where you can alter the JS before loading using this answer - https://stackoverflow.com/a/33854307/819866 (Not sure this is the way to go)

4) Used the setPageLoadStrategy parameter to make it work, but still in vain. https://stackoverflow.com/a/56789926/819866

Sunil Kumar
  • 622
  • 1
  • 12
  • 33
  • for Chrome it's easy, just use .back(). For Firefox I'm not sure, but you might consider removing the event handler by executing javascript here. Find "iframeNode" and remove the onLoad tag (or set the event to nothing..) – pcalkins Aug 16 '19 at 21:23
  • You've said everything correct. The only problem is that the control to the JS code (which removes the iframe) comes when the page is full loaded (because of which the onload event has triggered print command). I was expecting `setPageLoadStrategy` to work in this situation because it'll make not to wait for the page load to execute the next command. However, after setting `setPageLoadStrategy` the behaviour is the same. – Sunil Kumar Aug 17 '19 at 06:10
  • ahh, hadn't thought of that. The handler won't be added till ready state so you can't remove yet. Maybe try "eager" strategy and remove the iframe or create your own onload handler that removes all handlers from this. (no idea which would fire first...) Seems like that has a chance of executing before the handler is added. I think it would depend on the src of the iframe since "eager" is local DOM only. – pcalkins Aug 17 '19 at 17:40
  • I tried a few things and couldn't find a solution. Hopefully this will be addressed by "setUnhandledPromptBehaviour" in the next release of geckodriver. – pcalkins Aug 17 '19 at 21:40
  • Okay Thanks @pcalkins. Will wait for the next release of geckodriver. – Sunil Kumar Aug 18 '19 at 03:03
  • Seems like they need to change the browser so that this dialog is in the dom. One thing I didn't try is using java robot class. See this post: https://stackoverflow.com/questions/11537103/how-to-handle-print-dialog-in-selenium – pcalkins Aug 18 '19 at 16:57
  • Yes @pcalkins. Presently I'm using Robot only to press escape after page load. The problem with Robot is that it'll press escape without considering which window is on focus. So if someone minimises Firefox and opens up any window, Robot will keep pressing escape, which indirectly means that system should be idle when selenium runs. AutoIT seems to be solve this problem, I still have to look into AutoIT for this. – Sunil Kumar Aug 19 '19 at 06:44

1 Answers1

1

For Chrome browser print preview page controls are hidden in ShadowDOM therefore I would recommend clicking "Cancel" button by means of Selenium. Using Robot is not something I would suggest because if / when you will be running your test in Selenium Grid or in parallel - you will face issues as you will have to ensure that correct browser window is in focus now / switch focus between windows which is not that trivial

So I would recommend identifying the path to the "Cancel" button:

enter image description here

And traverse to it and click it. Example Java code:

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

and expandShadowRoot function which casts the given ShadowRoot to a Selenium's WebElement 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
  • Thanks for Google Chrome print window details. Can we identify the cancel button xpath in Firefox print window as well? If I do inspect element then it is not possible to identify the xpath of the window. Also, there can be two types of print window one is system print window, another one is Firefox customised print window. – Sunil Kumar Aug 15 '19 at 13:37