Unfortunately, WebDriver can’t handle these (or the other browser or OS dialog).
Moreover, they have an inclination to appear differently across browsers/systems/language settings, therefore there’s most likely no definite answer.
You’ll have to find and handle each doable case so as to make it work everywhere.
Your options include:
The Robot class, it allows you to “press” programmatically anything on the keyboard (or clicking blindly) and therefore getting rid of the dialog by, say, pressing Enter or Esc. However, as told above, any advanced interaction is dependant on OS/language/printer.
// press Escape programmatically - the print dialog should have focus, obviously
Robot r = new Robot();
r.keyPress(KeyEvent.VK_ESCAPE);
r.keyRelease(KeyEvent.VK_ESCAPE);
AutoIt. It’s a Windows program helpful for handling any system-level automation.
Same dependancy as above.
That’s more or less it.
If you'll be able to avoid the print dialog, try and take a screenshot of the page and print it using customary Java tools.
If you are interested to learn Selenium on a much deeper level and want to become a professional in the testing domain, check out Intellipaat’s Selenium online training!