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

查看:64
本文介绍了如何处理 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:

  • 机器人 类,它允许您以编程方式按下"键盘上的任何内容(或盲目点击),从而通过按下 EnterEsc 来摆脱对话框.但是,如上所述,任何高级交互都取决于操作系统/语言/打印机.

  • 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天全站免登陆