Explore Courses Blog Tutorials Interview Questions
0 votes
in DevOps and Agile by (19.7k points)

I have to handle print dialog (the same one that appears when clicking ctrl-p in browser). I tried with:

Alert printDialog = driver.switchTo().alert();


but it didn't work. Also I couldn't catch its window handle, because it's not a window.

Is it possible to handle these objects and how?

1 Answer

0 votes
by (62.9k points)

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



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!

Browse Categories