如何处理Selenium中的打印对话框? [英] How to handle print dialog in Selenium?

查看:127
本文介绍了如何处理Selenium中的打印对话框?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我必须处理打印对话框(在浏览器中单击ctrl-p时出现的对话框)。我尝试过:

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?

推荐答案

不幸的是,WebDriver无法处理这些(或任何其他浏览器或操作系统对话框)。而且,它们在浏览器/系统/语言设置方面往往看起来不同,因此可能没有明确的答案。您需要检测并处理每种可能的情况,以使其在任何地方都能正常工作。您的选择包括:

Unfortunately, WebDriver can't handle these (or any other browser or OS dialog). Moreover, they tend to look differently across browsers / systems / language settings, so there's probably no definite answer. You'll need to detect and handle every possible case in order to make it work everywhere. Your options include:


  • 机器人 类,它允许您以编程方式按下键盘上的任何内容(或者盲目地点击)然后通过按 Enter Esc 来摆脱对话框。但是,如上所述,任何高级交互都依赖于OS /语言/打印机。

  • The Robot class, it allows you to "press" programatically 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 programatically - the print dialog must have focus, obviously
Robot r = new Robot();
r.keyPress(KeyEvent.VK_ESCAPE);
r.keyRelease(KeyEvent.VK_ESCAPE);


  • AutoIt 。它是一个Windows程序,可用于处理任何系统级自动化。与上述相同。

  • AutoIt. It's a Windows program useful for handling any system-level automation. Same dependancy as above.

    这或多或少。如果您可以避开打印对话框,请尝试截取页面截图并使用标准使用标准打印Java工具

    That's more or less it. If you can avoid the print dialog, try to take screenshot of the page and print it using standard Java tools.

    这篇关于如何处理Selenium中的打印对话框?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

  • 查看全文
    登录 关闭
    扫码关注1秒登录
    发送“验证码”获取 | 15天全站免登陆