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