Intellipaat Back

Explore Courses Blog Tutorials Interview Questions
0 votes
2 views
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();

printDialog.dismiss();

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?

2 Answers

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

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!

0 votes
by (1.7k points)

As far as I know, we can't directly manipulate print dialogs with Selenium since this one is a system dialog. The two major options:

1. Disabling print dialog via browser settings through the preferences

For instance, in Chrome:

ChromeOptions options = new ChromeOptions();

options.addArguments(\"--kiosk-printing\");

2. With robot class

If you need to drive the dialog, use the Robot class from Java's utility package to simulate a mouse or keyboard action but using this is unreliable

Robot robot = new Robot();

robot.keyPress(KeyEvent.VK_ENTER);

robot.keyRelease(KeyEvent.VK_ENTER);

Instead, the recommended practice when automating is using preferences from the browser.

1.2k questions

2.7k answers

501 comments

693 users

Browse Categories

...