Java 如何在Selenium中处理打印对话框?
我必须处理打印对话框(与在浏览器中单击ctrl-p时显示的对话框相同)。我试过:Java 如何在Selenium中处理打印对话框?,java,selenium,webdriver,Java,Selenium,Webdriver,我必须处理打印对话框(与在浏览器中单击ctrl-p时显示的对话框相同)。我试过: Alert printDialog = driver.switchTo().alert(); printDialog.dismiss(); 但它不起作用。我也抓不到它的窗口把手,因为它不是一个窗口 有可能处理这些对象吗?如何处理?不幸的是,WebDriver无法处理这些对象(或任何其他浏览器或操作系统对话框)。此外,在不同的浏览器/系统/语言设置中,它们的外观往往不同,因此可能没有明确的答案。您需要检测和处理每一
Alert printDialog = driver.switchTo().alert();
printDialog.dismiss();
但它不起作用。我也抓不到它的窗口把手,因为它不是一个窗口
有可能处理这些对象吗?如何处理?不幸的是,WebDriver无法处理这些对象(或任何其他浏览器或操作系统对话框)。此外,在不同的浏览器/系统/语言设置中,它们的外观往往不同,因此可能没有明确的答案。您需要检测和处理每一个可能的案例,以便使其在任何地方都有效。你的选择包括:
- 在类中,它允许您通过编程方式“按”键盘上的任何内容(或盲目单击),因此可以通过按Enter或Esc来摆脱对话框。但是,如上所述,任何高级交互都取决于操作系统/语言/打印机
// press Escape programatically - the print dialog must have focus, obviously Robot r = new Robot(); r.keyPress(KeyEvent.VK_ESCAPE); r.keyRelease(KeyEvent.VK_ESCAPE);
- 。它是一个Windows程序,可用于处理任何系统级自动化。与上面相同的依赖性
差不多就是这样。如果可以避免打印对话框,请尝试拍摄页面的屏幕截图并打印出来。Slanec的回答是正确的-WebDriver没有此方面的本机功能。我在Windows中解决此问题的方法是使用System.Windows.Forms.SendKeys对象:
SendKeys.SendWait("^p");
System.Threading.Thread.Sleep(500);
SendKeys.SendWait("~");
// give it a minute to spool onto the printer
System.Threading.Thread.Sleep(5000);
事实上,我已经把它打印成了一堆语句。工作起来很有魅力。您只需在打印对话框出现后关闭浏览器即可。诚然,这是一项有点激进的措施,但好处是:
- 这很简单
- 它不需要任何外部工具
- 它可以跨操作系统和浏览器工作
myWebDriver.quit();
通常,您会运行测试,并像往常一样检查页面上要打印的元素。即使打印对话框打开,WebDriver也可以检查页面元素 完成后,退出浏览器(并为下一次测试启动一个新浏览器)
我们已经在一个项目中成功地使用了它。为了使事情变得更简单,我们编写了一个包装器类来跟踪“当前WebDriver实例”。我有方法检索这个实例(根据需要创建一个)并关闭它。这样,您就可以随时调用
getCurrentWebDriver()
并获取一个有效的实例(重复使用或新创建的)。使用Selenium和Facebook WebDriver with Codeception,我遇到了同样的问题–测试场景将停止运行,因为打印对话框阻止了与页面的任何交互
我最终没有在测试
环境中打开打印对话框(例如使用Symfony和Twig):
这样做的优点是不会停止测试。但是,它不允许测试打印对话框是否实际出现
作为冒烟测试,我添加了
$I->seeInirentURL('print=true')代码>(因为此URL参数触发window.print()
)。可以在Chrome中使用Selenium处理打印对话框。不确定其他浏览器
ChromeDriver-2.17中添加了“访问打印”对话框。详情可在此找到:
右键单击打印对话框->检查元素。因此,“打印的DOM”对话框将在单独的窗口中打开。现在,您可以为对话框中的任何元素生成定位器,并在测试中使用它们
要关闭“打印”对话框,可以执行以下操作:
//Switch to Print dialog
Set<String> windowHandles = driver.getWindowHandles();
if (!windowHandles.isEmpty()) {
driver.switchTo().window((String) windowHandles.toArray()[windowHandles.size() - 1]);
}
//Now work with the dialog as with an ordinary page:
driver.findElement(By.className("cancel")).click();
//切换到打印对话框
设置windowHandles=driver.getWindowHandles();
如果(!windowHandles.isEmpty()){
driver.switchTo().window((字符串)windowHandles.toArray()[windowHandles.size()-1]);
}
//现在,使用对话框就像使用普通页面一样:
driver.findElement(按.className(“取消”))。单击();
另一个选项是编辑Windows注册表以禁用Chrome打印窗口
--禁用打印预览-指示Chrome禁用打印预览的标志
转到注册表编辑器
导航到“计算机\HKEY\U类\U根目录\ChromeBHTML\shell\open\command”
将值设置为:
“C:\Program Files(x86)\Google\Chrome Beta\Application\Chrome.exe”--禁用打印预览
此解决方案适用于我的具体案例。我通过添加--kiosk printing
完全绕过打印对话框,解决了此问题。我的默认打印设置为pdf,所以我的下载文件夹中有一个pdf,但至少selenium没有挂起。下面是代码的样子:
ChromeOptions cOptions = new ChromeOptions();
cOptions.addArguments("kiosk-printing");
RemoteWebDriver driver = new RemoteWebDriver(hostUrl, cOptions);
我试着在一个不允许“全部打印”的网站上打印100多页有图案的列表。所以硒元素在理论上会自动帮我解决这个问题。
我为打印对话框窗口想出了一个完全非正统的解决方案
##Python code
import time
import threading
from pynput.mouse import Button, Controller
mouse = Controller()
##selenium starting up website stuff here
def website_print_button():
browser.find_element_by_id('printButton').click()
def push():
time.sleep(4) #4 second delay to give the computer time to open print dialog
mouse.position = [1131, 733] #where my print button is located
mouse.click(Button.left, 1)
def letsgo():
for x in range(printing_amount):
browser.find_element_by_xpath('/html/body/div[1]/form/table[4]/tbody/tr['+str(x+1)+']/td[1]/a/b').click()
browser.find_element_by_css_selector('ul.light-green.button').click()
browser.switch_to.window(browser.window_handles[1])
t2 = threading.Thread(target=push)
t2.start()
t1 = threading.Thread(target=website_print_button)
t1.start()
time.sleep(5)
browser.close()
browser.switch_to.window(browser.window_handles[0])
browser.find_element_by_class_name('c').click()
当打印对话框窗口打开时,Selenium通常会在等待某些事情发生时陷入困境。我使用了我最近学到的叫做“线程”的东西,这让我在技术上可以同时做两件事。我会要求在4秒钟内点击启动(按下功能),然后紧接着,Selenium会点击网站打印按钮。当Selenium打开“打印”对话框窗口时,它被卡在周围等待,但延迟的单击仍在运行,并在Selenium打开打印窗口4秒钟后等待单击。然后单击将执行,selenium重新获得控制权,因为打印窗口已被处理。Robot实际上工作得很好,满足了我的需要。我想我不能用“等到”来确定对话是否真的出现了?@zorglub76不,我不知道怎么做。如果你有什么想法,我很高兴听到你的话!我在静态变量VK_ESCAPE
中遇到错误,只需导入import java.awt.event.KeyEvent即可解决代码>Robot类可能无法工作,因为WebDriver打开的浏览器窗口通常没有(窗口)焦点。在这种情况下,打印对话框也没有焦点,因此机器人模拟的按键无法到达。您可以先尝试聚焦窗口(例如使用Javascript),但这意味着
##Python code
import time
import threading
from pynput.mouse import Button, Controller
mouse = Controller()
##selenium starting up website stuff here
def website_print_button():
browser.find_element_by_id('printButton').click()
def push():
time.sleep(4) #4 second delay to give the computer time to open print dialog
mouse.position = [1131, 733] #where my print button is located
mouse.click(Button.left, 1)
def letsgo():
for x in range(printing_amount):
browser.find_element_by_xpath('/html/body/div[1]/form/table[4]/tbody/tr['+str(x+1)+']/td[1]/a/b').click()
browser.find_element_by_css_selector('ul.light-green.button').click()
browser.switch_to.window(browser.window_handles[1])
t2 = threading.Thread(target=push)
t2.start()
t1 = threading.Thread(target=website_print_button)
t1.start()
time.sleep(5)
browser.close()
browser.switch_to.window(browser.window_handles[0])
browser.find_element_by_class_name('c').click()